Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Yang authored and Jim Yang committed Jul 13, 2021
0 parents commit 7c161f2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
cmake*
CMakeLists.txt
main
61 changes: 61 additions & 0 deletions FunctionTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef FUNCTIONTIMER_H
#define FUNCTIONTIMER_H

#include <iostream>
#include <limits>
#include <sys/time.h>

class FunctionStats {
private:
int64_t longest_time = 0; // the longest function running time
int64_t shortest_time = std::numeric_limits<int>::max(); // the shortest function running time
int64_t total_time = 0;
int64_t count = 0; // number of times the function being called
std::string time_unit;

public:
FunctionStats(std::string time_unit_): time_unit(time_unit_){}

void update_time(int running_time) {
++count;
total_time += running_time;

if (running_time > longest_time) longest_time = running_time;
if (running_time < shortest_time) shortest_time = running_time;
}

void print_stats() const{
std::cout << "=========================================================" << std::endl;
std::cout << "this function has run " << count << " times" << std::endl;
std::cout << "average function running time: " << (int64_t)(total_time / count) << time_unit << std::endl;
std::cout << "longest function running time: " << longest_time << time_unit << std::endl;
std::cout << "shortest function running time: " << shortest_time << time_unit << std::endl;
std::cout << "=========================================================" << std::endl;
}
};

// time the function for running once, default unit is millisecond
class FunctionTimer {
private:
timeval start;
timeval end;

public:
void start_timer () {
gettimeofday(&start, NULL);
}

void calculate_time() {
gettimeofday(&end, NULL);
}

int64_t get_elapsed_time_in_milliseconds() const {
return (end.tv_usec - start.tv_usec) / 1000;
}

int64_t get_elapsed_time_in_microseconds() const {
return end.tv_usec - start.tv_usec;
}
};

#endif
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CC=g++

main: main.cpp
${CC} -o $@ $^

clean:
rm -f main
28 changes: 28 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include "FunctionTimer.h"

using namespace std;

FunctionStats STAT("u");

void func() {
int x = 0;
for (int i = 0; i < 1000000; ++i) {
x += 1;
}
}

int main() {
FunctionTimer timer(1000000);
cout << "Program starts" << endl;

for (int i = 0; i < 10; ++i) {
timer.start_timer();
func();
timer.calculate_time();
STAT.update_time(timer.get_elapsed_time());
}
STAT.print_stats();

return 0;
}

0 comments on commit 7c161f2

Please sign in to comment.