aboutsummaryrefslogtreecommitdiffstats
path: root/native/include/cpu_time.h
blob: 0efea8b8ecec486ce19fc235e5c2bd472d98400e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef CPU_TIME_H
#define CPU_TIME_H

#include <iostream>

// How much CPU time used (in seconds)?
double get_cpu_usage(void);

class CPUClock
{
private:
	const char *name;
	const char *func;

	unsigned int count;

	double start_time;
	double last;
	double total;

public:
	CPUClock(const char *_name = 0, const char *_func = 0)
		: name(_name), func(_func),
		  count(0), start_time(0), last(0), total(0)
	{}

	void start()
	{
		start_time = get_cpu_usage();
	}

	void stop()
	{
		last = get_cpu_usage() - start_time;
		total += last;
		count++;
	}

	double get_total() const
	{
		return total;
	}

	double get_last() const
	{
		return last;
	}

	double get_count() const
	{
		return count;
	}

	double get_average() const
	{
		return total / ( count ? count : 1);
	}

	const char *get_name() const
	{
		return name;
	}

	const char *get_function() const
	{
		return func;
	}
};

std::ostream& operator<<(std::ostream &os, const CPUClock &clock);

char* strip_types(const char* pretty_func);

#define DEFINE_CPU_CLOCK(var) CPUClock var = CPUClock(#var, strip_types(__PRETTY_FUNCTION__))

#endif