-
Notifications
You must be signed in to change notification settings - Fork 208
/
Timer.cpp
181 lines (142 loc) · 4.25 KB
/
Timer.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch).
* Copyright (c) 2019 Jean Luc PONS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Timer.h"
static const char *prefix[] = { "","Kilo","Mega","Giga","Tera","Peta","Hexa" };
#ifdef WIN64
LARGE_INTEGER Timer::perfTickStart;
double Timer::perfTicksPerSec;
LARGE_INTEGER Timer::qwTicksPerSec;
#include <wincrypt.h>
#else
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
time_t Timer::tickStart;
#endif
void Timer::Init() {
#ifdef WIN64
QueryPerformanceFrequency(&qwTicksPerSec);
QueryPerformanceCounter(&perfTickStart);
perfTicksPerSec = (double)qwTicksPerSec.QuadPart;
#else
tickStart=time(NULL);
#endif
}
double Timer::get_tick() {
#ifdef WIN64
LARGE_INTEGER t, dt;
QueryPerformanceCounter(&t);
dt.QuadPart = t.QuadPart - perfTickStart.QuadPart;
return (double)(dt.QuadPart) / perfTicksPerSec;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)(tv.tv_sec - tickStart) + (double)tv.tv_usec / 1e6;
#endif
}
std::string Timer::getSeed(int size) {
std::string ret;
char tmp[3];
unsigned char *buff = (unsigned char *)malloc(size);
#ifdef WIN64
HCRYPTPROV hCryptProv = NULL;
LPCSTR UserName = "KeyContainer";
if (!CryptAcquireContext(
&hCryptProv, // handle to the CSP
UserName, // container name
NULL, // use the default provider
PROV_RSA_FULL, // provider type
0)) // flag values
{
//-------------------------------------------------------------------
// An error occurred in acquiring the context. This could mean
// that the key container requested does not exist. In this case,
// the function can be called again to attempt to create a new key
// container. Error codes are defined in Winerror.h.
if (GetLastError() == NTE_BAD_KEYSET) {
if (!CryptAcquireContext(
&hCryptProv,
UserName,
NULL,
PROV_RSA_FULL,
CRYPT_NEWKEYSET)) {
printf("CryptAcquireContext(): Could not create a new key container.\n");
exit(1);
}
} else {
printf("CryptAcquireContext(): A cryptographic service handle could not be acquired.\n");
exit(1);
}
}
if (!CryptGenRandom(hCryptProv,size,buff)) {
printf("CryptGenRandom(): Error during random sequence acquisition.\n");
exit(1);
}
CryptReleaseContext(hCryptProv, 0);
#else
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);
#endif
for (int i = 0; i < size; i++) {
sprintf(tmp,"%02X",buff[i]);
ret.append(tmp);
}
free(buff);
return ret;
}
uint32_t Timer::getSeed32() {
return ::strtoul(getSeed(4).c_str(),NULL,16);
}
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() {
#ifdef WIN64
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#else
// TODO
return 1;
#endif
}
void Timer::SleepMillis(uint32_t millis) {
#ifdef WIN64
Sleep(millis);
#else
usleep(millis*1000);
#endif
}