1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
27 * Simple OS time measurement abstraction.
36 #elif defined(__linux__)
38 #elif defined(__APPLE__)
39 #include <mach/mach_time.h>
47 // OS dependent time frequency
48 #if defined(_WIN32) || defined(__APPLE__)
49 // runtime variable on Windows and MacOSX
50 extern long long timeFrequency;
51 #elif defined(__linux__)
52 // nanoseconds on Linux
53 static const long long timeFrequency = 1000000000LL;
55 // microseconds on Unices
56 static const long long timeFrequency = 1000000LL;
59 // Time from an unknown base in a unit determined by timeFrequency
64 LARGE_INTEGER frequency;
65 QueryPerformanceFrequency(&frequency);
66 timeFrequency = frequency.QuadPart;
68 LARGE_INTEGER counter;
69 QueryPerformanceCounter(&counter);
70 return counter.QuadPart;
71 #elif defined(__linux__)
73 if (clock_gettime(CLOCK_REALTIME, &tp) == -1) {
76 return tp.tv_sec * 1000000000LL + tp.tv_nsec;
77 #elif defined(__APPLE__)
79 mach_timebase_info_data_t timebaseInfo;
80 mach_timebase_info(&timebaseInfo);
81 timeFrequency = 1000000000LL * timebaseInfo.denom / timebaseInfo.numer;
83 return mach_absolute_time();
86 gettimeofday(&tv, NULL);
87 return tv.tv_sec * 1000000LL + tv.tv_usec;
94 #endif /* _OS_TIME_HPP_ */