-
Notifications
You must be signed in to change notification settings - Fork 11
/
db_cputime.c
66 lines (54 loc) · 1.42 KB
/
db_cputime.c
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
#ifndef _WIN32
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>
double getCpuTime(int type) {
struct rusage used[1];
struct timeval tv[1];
switch( type ) {
case 0:
gettimeofday(tv, NULL);
return (double)tv->tv_sec + (double)tv->tv_usec / 1000000;
case 1:
getrusage(RUSAGE_SELF, used);
return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
case 2:
getrusage(RUSAGE_SELF, used);
return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
}
return 0;
}
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <process.h>
double getCpuTime(int type) {
FILETIME crtime[1];
FILETIME xittime[1];
FILETIME systime[1];
FILETIME usrtime[1];
SYSTEMTIME timeconv[1];
double ans = 0;
memset (timeconv, 0, sizeof(SYSTEMTIME));
switch( type ) {
case 0:
GetSystemTimeAsFileTime (xittime);
FileTimeToSystemTime (xittime, timeconv);
ans = (double)timeconv->wDayOfWeek * 3600 * 24;
break;
case 1:
GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
FileTimeToSystemTime (usrtime, timeconv);
break;
case 2:
GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
FileTimeToSystemTime (systime, timeconv);
break;
}
ans += (double)timeconv->wHour * 3600;
ans += (double)timeconv->wMinute * 60;
ans += (double)timeconv->wSecond;
ans += (double)timeconv->wMilliseconds / 1000;
return ans;
}
#endif