-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutimer.cpp
More file actions
46 lines (36 loc) · 1.52 KB
/
utimer.cpp
File metadata and controls
46 lines (36 loc) · 1.52 KB
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
#include <iostream>
#include <chrono>
#include <thread>
#define START(timename) auto timename = std::chrono::system_clock::now();
#define STOP(timename, elapsed) auto elapsed = std::chrono::duration_cast < std::chrono::microseconds > (std::chrono::system_clock::now() - timename).count();
class utimer {
std::chrono::system_clock::time_point start;
std::chrono::system_clock::time_point stop, pause;
std::string message;
using usecs = std::chrono::microseconds;
using msecs = std::chrono::milliseconds;
private:
long * us_elapsed;
public:
utimer(const std::string m): message(m), us_elapsed((long * ) NULL) {
start = std::chrono::system_clock::now();
}
utimer(const std::string m, long * us): message(m), us_elapsed(us) {
start = std::chrono::system_clock::now();
}
~utimer() {
stop = std::chrono::system_clock::now();
std::chrono::duration < double > elapsed = stop - start;
auto musec = std::chrono::duration_cast < std::chrono::microseconds > (elapsed).count();
std::cout << message << " computed in " << musec << " usec " <<
std::endl;
if (us_elapsed != NULL)
( * us_elapsed) = musec;
}
auto elapsed() {
pause = std::chrono::system_clock::now();
std::chrono::duration < double > elapsed = pause - start;
auto musec = std::chrono::duration_cast < std::chrono::microseconds > (elapsed).count();
return musec;
}
};