aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--native/include/cpu_time.h69
-rw-r--r--native/src/cpu_time.cpp30
2 files changed, 99 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)?
5double get_cpu_usage(void); 7double get_cpu_usage(void);
6 8
9class CPUClock
10{
11private:
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
21public:
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
70std::ostream& operator<<(std::ostream &os, const CPUClock &clock);
71
72char* 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
diff --git a/native/src/cpu_time.cpp b/native/src/cpu_time.cpp
index d003f22..8dde662 100644
--- a/native/src/cpu_time.cpp
+++ b/native/src/cpu_time.cpp
@@ -3,6 +3,8 @@
3#include <sys/time.h> 3#include <sys/time.h>
4#include <sys/resource.h> 4#include <sys/resource.h>
5 5
6#include <cstring>
7
6#include "cpu_time.h" 8#include "cpu_time.h"
7 9
8 10
@@ -47,3 +49,31 @@ double get_cpu_usage(void)
47} 49}
48 50
49#endif 51#endif
52
53
54std::ostream& operator<<(std::ostream &os, const CPUClock &clock)
55{
56 if (clock.get_function())
57 os << clock.get_function() << "::";
58 os << clock.get_name()
59 << ": total=" << clock.get_total() * 1000 << "ms "
60 << "last=" << clock.get_last() * 1000 << "ms "
61 << "average=" << clock.get_average() * 1000 << "ms "
62 << "count=" << clock.get_count();
63 return os;
64}
65
66char *strip_types(const char* pretty_func)
67{
68 char *copy = strdup(pretty_func);
69
70 char *start = strchr(copy, ' ');
71 char *end = strchr(copy, '(');
72
73 if (start)
74 copy = start + 1;
75 if (end)
76 *end = '\0';
77
78 return copy;
79}