diff options
| author | Bjoern Brandenburg <bbb@mpi-sws.org> | 2012-08-07 05:03:56 -0400 |
|---|---|---|
| committer | Bjoern Brandenburg <bbb@mpi-sws.org> | 2013-02-12 06:49:39 -0500 |
| commit | 9eb76ebd3e970c3e37ce95ec6e21242a3b0ee0e4 (patch) | |
| tree | ec68455719f33fb94420a3b00dfa8020109eb255 /native/include | |
| parent | 4b07d5bcd33e4b0433d4b91f9268a54399df676b (diff) | |
Add benchmarking helper class
CPUClock is useful for repeatedly measuring how long
some piece of code executes.
Diffstat (limited to 'native/include')
| -rw-r--r-- | native/include/cpu_time.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/native/include/cpu_time.h b/native/include/cpu_time.h index 2484159..0efea8b 100644 --- a/native/include/cpu_time.h +++ b/native/include/cpu_time.h | |||
| @@ -1,7 +1,76 @@ | |||
| 1 | #ifndef CPU_TIME_H | 1 | #ifndef CPU_TIME_H |
| 2 | #define CPU_TIME_H | 2 | #define CPU_TIME_H |
| 3 | 3 | ||
| 4 | #include <iostream> | ||
| 5 | |||
| 4 | // How much CPU time used (in seconds)? | 6 | // How much CPU time used (in seconds)? |
| 5 | double get_cpu_usage(void); | 7 | double get_cpu_usage(void); |
| 6 | 8 | ||
| 9 | class CPUClock | ||
| 10 | { | ||
| 11 | private: | ||
| 12 | const char *name; | ||
| 13 | const char *func; | ||
| 14 | |||
| 15 | unsigned int count; | ||
| 16 | |||
| 17 | double start_time; | ||
| 18 | double last; | ||
| 19 | double total; | ||
| 20 | |||
| 21 | public: | ||
| 22 | CPUClock(const char *_name = 0, const char *_func = 0) | ||
| 23 | : name(_name), func(_func), | ||
| 24 | count(0), start_time(0), last(0), total(0) | ||
| 25 | {} | ||
| 26 | |||
| 27 | void start() | ||
| 28 | { | ||
| 29 | start_time = get_cpu_usage(); | ||
| 30 | } | ||
| 31 | |||
| 32 | void stop() | ||
| 33 | { | ||
| 34 | last = get_cpu_usage() - start_time; | ||
| 35 | total += last; | ||
| 36 | count++; | ||
| 37 | } | ||
| 38 | |||
| 39 | double get_total() const | ||
| 40 | { | ||
| 41 | return total; | ||
| 42 | } | ||
| 43 | |||
| 44 | double get_last() const | ||
| 45 | { | ||
| 46 | return last; | ||
| 47 | } | ||
| 48 | |||
| 49 | double get_count() const | ||
| 50 | { | ||
| 51 | return count; | ||
| 52 | } | ||
| 53 | |||
| 54 | double get_average() const | ||
| 55 | { | ||
| 56 | return total / ( count ? count : 1); | ||
| 57 | } | ||
| 58 | |||
| 59 | const char *get_name() const | ||
| 60 | { | ||
| 61 | return name; | ||
| 62 | } | ||
| 63 | |||
| 64 | const char *get_function() const | ||
| 65 | { | ||
| 66 | return func; | ||
| 67 | } | ||
| 68 | }; | ||
| 69 | |||
| 70 | std::ostream& operator<<(std::ostream &os, const CPUClock &clock); | ||
| 71 | |||
| 72 | char* strip_types(const char* pretty_func); | ||
| 73 | |||
| 74 | #define DEFINE_CPU_CLOCK(var) CPUClock var = CPUClock(#var, strip_types(__PRETTY_FUNCTION__)) | ||
| 75 | |||
| 7 | #endif | 76 | #endif |
