-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimer.cpp
More file actions
103 lines (82 loc) · 2.17 KB
/
Timer.cpp
File metadata and controls
103 lines (82 loc) · 2.17 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "Timer.h"
static const char *prefix[] = { "","Kilo","Mega","Giga","Tera","Peta","Hexa" };
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
time_t Timer::tickStart;
void Timer::Init() {
tickStart=time(NULL);
}
double Timer::get_tick() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)(tv.tv_sec - tickStart) + (double)tv.tv_usec / 1e6;
}
uint32_t Timer::getSeed32() {
return ::strtoul(getSeed(4).c_str(),NULL,16);
}
uint32_t Timer::getPID() {
return (uint32_t)getpid();
}
std::string Timer::getSeed(int size) {
std::string ret;
char tmp[3];
unsigned char *buff = (unsigned char *)malloc(size);
FILE *f = fopen("/dev/urandom","rb");
if(f==NULL) {
printf("Failed to open /dev/urandom %s\n", strerror( errno ));
exit(1);
}
if( fread(buff,1,size,f)!=size ) {
printf("Failed to read from /dev/urandom %s\n", strerror( errno ));
exit(1);
}
fclose(f);
for (int i = 0; i < size; i++) {
sprintf(tmp,"%02X",buff[i]);
ret.append(tmp);
}
free(buff);
return ret;
}
std::string Timer::getResult(char *unit, int nbTry, double t0, double t1) {
char tmp[256];
int pIdx = 0;
double nbCallPerSec = (double)nbTry / (t1 - t0);
while (nbCallPerSec > 1000.0 && pIdx < 5) {
pIdx++;
nbCallPerSec = nbCallPerSec / 1000.0;
}
sprintf(tmp, "%.3f %s%s/sec", nbCallPerSec, prefix[pIdx], unit);
return std::string(tmp);
}
void Timer::printResult(char *unit, int nbTry, double t0, double t1) {
printf("%s\n", getResult(unit, nbTry, t0, t1).c_str());
}
int Timer::getCoreNumber() {
return (size_t)sysconf(_SC_NPROCESSORS_ONLN);
}
void Timer::SleepMillis(uint32_t millis) {
usleep(millis*1000);
}
std::string Timer::getTS() {
std::string ret;
time_t now = time(NULL);
char *time = ctime(&now);
if(time[8]==' ') time[8]='0';
ret.push_back(time[8]);
ret.push_back(time[9]);
ret.push_back(time[4]);
ret.push_back(time[5]);
ret.push_back(time[6]);
ret.push_back(time[22]);
ret.push_back(time[23]);
ret.push_back('_');
ret.push_back(time[11]);
ret.push_back(time[12]);
ret.push_back(time[14]);
ret.push_back(time[15]);
ret.push_back(time[17]);
ret.push_back(time[18]);
return ret;
}