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";
97 std::cerr << msg << std::endl;
102 // Set the start time
106 gettimeofday(&tv, &tz);
107 tm = localtime(&tv.tv_sec);
108 start_hour = tm->tm_hour;
109 start_min = tm->tm_min;
110 start_sec = tm->tm_sec;
111 start_usec = tv.tv_usec;
113 QueryPerformanceCounter(&iStart);
117 //===========================================================================
118 // Stop the timer and print an optional message.
120 //===========================================================================
121 // Print out an optional message followed by the current timer timing.
123 inline double check(const char* msg, bool _bRestart = false)
125 // Print an optional message, something like "Checking timer t";
126 double t = elapsed_time();
129 std::cerr << "[" << msg << "]" << " : ";
131 std::cerr << "Elapsed time ["
132 << std::setiosflags(std::ios::fixed)
133 << std::setprecision(3)
134 << t << "] milliseconds"
137 if (_bRestart == true)
146 //===========================================================================
147 // Allow timers to be printed to ostreams using the syntax 'os << t'
148 // for an ostream 'os' and a timer 't'. For example, "cout << t" will
149 // print out the total amount of time 't' has been "running".
151 inline std::ostream& operator<<(std::ostream& os, Timer& t)
153 os << std::setprecision(3)
154 << std::setiosflags(std::ios::fixed)
155 << t.elapsed_time() ;
159 //===========================================================================
160 #endif /* !__TIMER_HXX__ */