Repository navigation
Expand file tree
/
Copy pathget_sec.cpp
More file actions
162 lines (144 loc) · 3.84 KB
/
Copy pathget_sec.cpp
File metadata and controls
162 lines (144 loc) · 3.84 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
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
/**********************************************/
/* get_sec.cpp: */
/* Copyright (C) 2015 Tomonori Kouya */
/* */
/* This library is free software; you can re- */
/* distribute it and/or modify it under the */
/* terms of the GNU Lesser General Public */
/* License as published by the Free Software */
/* Foundation; either version 2.1 of the */
/* License, or (at your option) any later */
/* version. */
/* */
/* This library 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 Lesser */
/* General Public License for more details. */
/**********************************************/
#include <cstdio>
#include <time.h>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/times.h>
#include <sys/time.h>
#ifndef CLK_TCK
#include <unistd.h>
#define CLK_TCK (sysconf(_SC_CLK_TCK))
#endif
#endif
#define DIVTIMES 2
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
//#define USE_CLOCK
/* flag == 0: No print */
double get_sec(int flag)
{
#ifdef WIN32
static int first = 1;
static LARGE_INTEGER _tstart;
static LARGE_INTEGER freq;
if(first) {
QueryPerformanceFrequency(&freq);
first = 0;
}
QueryPerformanceCounter(&_tstart);
return ((double)_tstart.QuadPart)/((double)freq.QuadPart);
#else
double ret;
struct tms tmp;
#ifdef USE_CLOCK
if(flag != 0)
printf("Time : %d / %d\n", (int)clock(), CLOCKS_PER_SEC);
ret = (double)(clock()) / CLOCKS_PER_SEC;
#else
#ifdef USE_MPFRTIME
//int
//cputime ()
//{
#include <sys/types.h>
#include <sys/resource.h>
//
struct rusage rus;
getrusage (0, &rus);
//return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
ret = rus.ru_utime.tv_sec + rus.ru_utime.tv_usec / 1000 / 1000;
//}
#else
times(&tmp);
if(flag != 0)
{
printf("User Time : %ld / %ld\n", tmp.tms_utime, CLK_TCK);
printf("System Time: %ld / %ld\n", tmp.tms_stime, CLK_TCK);
printf("CUser Time : %ld / %ld\n", tmp.tms_cutime, CLK_TCK);
printf("CSystem Time: %ld / %ld\n", tmp.tms_cstime, CLK_TCK);
printf("Ret(utime+stime) : %g\n", (double)(tmp.tms_utime + tmp.tms_stime) / CLK_TCK);
printf("Ret(cutime+cstime) : %g\n", (double)(tmp.tms_cutime + tmp.tms_cstime) / CLK_TCK);
}
ret = (double)(tmp.tms_utime + tmp.tms_stime) / CLK_TCK;
#endif
#endif
return ret;
#endif
}
/* double get_secv(void) */
double get_secv(void)
{
return get_sec(0);
}
/* float fget_sec(int flag) */
float fget_sec(int flag)
{
return (float)get_sec(flag);
}
/* float fget_sec(void) */
float fget_secv(void)
{
return (float)get_sec(0);
}
/* get REAL time */
/* for Multi-threads programming */
double get_real_sec(int flag)
{
#ifdef WIN32
static int first = 1;
static LARGE_INTEGER _tstart;
static LARGE_INTEGER freq;
if(first) {
QueryPerformanceFrequency(&freq);
first = 0;
}
QueryPerformanceCounter(&_tstart);
return ((double)_tstart.QuadPart)/((double)freq.QuadPart);
#else
double ret;
struct timeval tmp;
gettimeofday(&tmp, NULL);
if(flag != 0)
{
printf("tv_sec : %ld\n", tmp.tv_sec);
printf("tv_usec: %ld\n", tmp.tv_usec);
printf("Ret : %g\n", (double)tmp.tv_sec + (double)tmp.tv_usec / 1000.0 / 1000.0);
}
ret = (double)tmp.tv_sec + (double)tmp.tv_usec / 1000.0 / 1000.0;
return ret;
#endif
}
double get_real_secv(void)
{
return get_real_sec(0);
}
float fget_real_sec(int flag)
{
return (float)get_real_sec(flag);
}
float fget_real_secv(void)
{
return (float)get_real_sec(0);
}
#ifdef __cplusplus
} // extern "C" {
#endif // __cplusplus