2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008-2008 - DIGITEO - Bruno JOFRET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
20 //#include <winbase.h>
42 //===========================================================================
43 // Return the total time that the timer has been in the "running"
44 // state since it was first "started" or last "restarted". For
45 // "short" time periods (less than an hour), the actual cpu time
46 // used is reported instead of the elapsed time.
48 inline double elapsed_time(bool _bRestart = false)
55 gettimeofday(&tv, &tz);
56 tm = localtime(&tv.tv_sec);
57 double dblTime = ( 3600000.0 * (tm->tm_hour - start_hour) +
58 60000.0 * (tm->tm_min - start_min) +
59 1000.0 * (tm->tm_sec - start_sec) +
60 (1.0 * (tv.tv_usec - start_usec)) / 1000.0
63 QueryPerformanceCounter(&iEnd);
64 double dblTime = ((iEnd.QuadPart - iStart.QuadPart) * 1000.0) / iFreq.QuadPart;
67 if (_bRestart == true)
72 } // timer::elapsed_time
84 QueryPerformanceFrequency(&iFreq);
88 //===========================================================================
89 // Start a timer. If it is already running, let it continue running.
90 // Print an optional message.
92 inline void start(const char* msg = 0)
94 // Print an optional message, something like "Starting timer t";
95 if (msg) std::cerr << msg << std::endl;
103 gettimeofday(&tv, &tz);
104 tm = localtime(&tv.tv_sec);
105 start_hour = tm->tm_hour;
106 start_min = tm->tm_min;
107 start_sec = tm->tm_sec;
108 start_usec = tv.tv_usec;
110 QueryPerformanceCounter(&iStart);
114 //===========================================================================
115 // Stop the timer and print an optional message.
117 //===========================================================================
118 // Print out an optional message followed by the current timer timing.
120 inline double check(const char* msg, bool _bRestart = false)
122 // Print an optional message, something like "Checking timer t";
123 double t = elapsed_time();
124 if (msg) std::cerr << "[" << msg << "]" << " : ";
125 std::cerr << "Elapsed time ["
126 << std::setiosflags(std::ios::fixed)
127 << std::setprecision(3)
128 << t << "] milliseconds"
131 if (_bRestart == true)
140 //===========================================================================
141 // Allow timers to be printed to ostreams using the syntax 'os << t'
142 // for an ostream 'os' and a timer 't'. For example, "cout << t" will
143 // print out the total amount of time 't' has been "running".
145 inline std::ostream& operator<<(std::ostream& os, Timer& t)
147 os << std::setprecision(3)
148 << std::setiosflags(std::ios::fixed)
149 << t.elapsed_time() ;
153 //===========================================================================
154 #endif /* !__TIMER_HXX__ */