Initial import into git.
[galago.git] / cpp / galago / contrib / indri / src / IndriTimer.cpp
blobd6cddc69be2b2337f3baa66b811b3fb81132696a
1 /*==========================================================================
2 * Copyright (c) 2004 University of Massachusetts. All Rights Reserved.
4 * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
5 * is subject to the terms of the software license set forth in the LICENSE
6 * file included with this software, and also available at
7 * http://www.lemurproject.org/license.html
9 *==========================================================================
14 // IndriTimer
16 // 13 August 2004 -- tds
19 #include "indri/IndriTimer.hpp"
20 #include "lemur/lemur-compat.hpp"
21 #include <iomanip>
22 #ifndef WIN32
23 #include <sys/time.h>
24 #endif
27 // IndriTimer
30 indri::utility::IndriTimer::IndriTimer()
32 _start(0),
33 _elapsed(0),
34 _stopped(true)
39 // currentTime
42 UINT64 indri::utility::IndriTimer::currentTime() {
43 #ifdef WIN32
44 FILETIME filetime;
45 ::GetSystemTimeAsFileTime( &filetime );
47 // this time is now in 100 nanosecond increments
48 UINT64 result = filetime.dwHighDateTime;
49 result <<= 32;
50 result += filetime.dwLowDateTime;
52 return result / 10;
53 #else
54 struct timeval tv;
55 gettimeofday(&tv, 0);
56 UINT64 seconds = tv.tv_sec;
57 UINT64 million = 1000000;
58 UINT64 microseconds = tv.tv_usec;
60 return seconds * million + microseconds;
61 #endif
65 // start
68 void indri::utility::IndriTimer::start() {
69 _stopped = false;
70 _start = currentTime();
74 // stop
77 void indri::utility::IndriTimer::stop() {
78 _elapsed += (currentTime() - _start);
79 _start = 0;
80 _stopped = true;
84 // reset
87 void indri::utility::IndriTimer::reset() {
88 _stopped = true;
89 _start = 0;
90 _elapsed = 0;
94 // elapsedTime
97 UINT64 indri::utility::IndriTimer::elapsedTime() const {
98 UINT64 total = _elapsed;
100 if( !_stopped ) {
101 total += (currentTime() - _start);
104 return total;
108 // printElapsedMicroseconds
111 void indri::utility::IndriTimer::printElapsedMicroseconds( std::ostream& out ) const {
112 UINT64 elapsed = elapsedTime();
113 const UINT64 million = 1000000;
115 int minutes = int(elapsed / (60 * million));
116 int seconds = int(elapsed/million - 60*minutes);
117 int microseconds = int(elapsed % million);
119 out << minutes
120 << ":"
121 << std::setw(2) << std::setfill('0')
122 << seconds
123 << "."
124 << std::setw(6) << std::setfill('0')
125 << microseconds;
129 // printElapsedSeconds
132 void indri::utility::IndriTimer::printElapsedSeconds( std::ostream& out ) const {
133 UINT64 elapsed = elapsedTime();
134 const UINT64 million = 1000000;
136 int minutes = int(elapsed / (60 * million));
137 int seconds = int(elapsed/million - 60*minutes);
138 int microseconds = int(elapsed % million);
140 out << minutes
141 << ":"
142 << std::setw(2) << std::setfill('0')
143 << seconds;