Debian Codegraph @

src/Libpfs/utils/msec_timer.cpp

raw
1 /**
2 * @brief Milliseconds Timer
3 *
4 * ----------------------------------------------------------------------
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * ----------------------------------------------------------------------
19 *
20 * http://msdn.microsoft.com/en-us/library/ms644904%28v=VS.85%29.aspx
21 *
22 * @author Davide Anastasia, <[email protected]>
23 *
24 */
25
26 #include "msec_timer.h"
27
28 // costructor
29 msec_timer::msec_timer()
30 :
31 #ifdef WIN_TIMER
32 wrk_time(0.0)
33 #elif __APPLE__
34 start_t(0),
35 stop_t(0),
36 wrk_time(0),
37 conversion(1.0)
38 #else
39 wrk_time(0.0)
40 #endif
41 {
42 #ifdef WIN_TIMER
43 QueryPerformanceFrequency(&freq);
44 #elif __APPLE__
45 mach_timebase_info_data_t info;
46 kern_return_t err = mach_timebase_info(&info);
47
48 // Convert the timebase into seconds
49 if (err == 0) conversion = (1e-9 * (double)info.numer / (double)info.denom);
50 #else
51
52 #endif
53 }
54
55 msec_timer::~msec_timer() // destructor
56 {
57 // nothing to do
58 }
59
60 void msec_timer::start() {
61 #ifdef WIN_TIMER
62 QueryPerformanceCounter(&start_t);
63 #elif __APPLE__
64 start_t = mach_absolute_time();
65 #else
66 gettimeofday(&start_t, NULL);
67 #endif
68 }
69
70 void msec_timer::stop() {
71 #ifdef WIN_TIMER
72 QueryPerformanceCounter(&stop_t);
73 #elif __APPLE__
74 stop_t = mach_absolute_time();
75 #else
76 gettimeofday(&stop_t, NULL);
77 #endif
78 }
79
80 void msec_timer::update() {
81 #ifdef WIN_TIMER
82 wrk_time += ((double)(stop_t.QuadPart - start_t.QuadPart));
83 #elif __APPLE__
84 wrk_time += stop_t - start_t;
85 #else
86 wrk_time += (((stop_t.tv_sec - start_t.tv_sec) * 1000.0) +
87 (stop_t.tv_usec - start_t.tv_usec) / 1000.0);
88 #endif
89 }
90
91 void msec_timer::stop_and_update() {
92 stop();
93 update();
94 }
95
96 void msec_timer::reset() { wrk_time = 0.0; }
97
98 double msec_timer::get_time() {
99 #ifdef WIN_TIMER
100 return (wrk_time * 1000.0 / freq.QuadPart);
101 #elif __APPLE__
102 return (conversion * (double)wrk_time * 1000.0);
103 #else
104 return wrk_time;
105 #endif
106 }
107
108 void msec_timer::get_timer_type() {
109 #ifdef WIN_TIMER
110 printf("<windows.h> QueryPerformanceCounter()\n");
111 #elif __APPLE__
112 printf("<mach/mach_time.h> mach_absolute_time()\n");
113 #else
114 // clock_gettime(3)
115 printf("<sys/time.h> gettimeofday()\n");
116 #endif
117 }
118
119 double convert_to_gigaflops(double msec_time, double scale_factor) {
120 return ((double)scale_factor / (msec_time * 1000.0));
121 }