-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jim Yang
authored and
Jim Yang
committed
Jul 13, 2021
0 parents
commit 7c161f2
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
cmake* | ||
CMakeLists.txt | ||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |