time.cc (1431B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #include "lib/extras/time.h" 7 8 #include <stdint.h> 9 #include <stdlib.h> 10 11 #include <ctime> 12 13 #include "lib/jxl/base/os_macros.h" // for JXL_OS_* 14 15 #if JXL_OS_WIN 16 #ifndef NOMINMAX 17 #define NOMINMAX 18 #endif // NOMINMAX 19 #include <windows.h> 20 #endif // JXL_OS_WIN 21 22 #if JXL_OS_MAC 23 #include <mach/mach.h> 24 #include <mach/mach_time.h> 25 #endif // JXL_OS_MAC 26 27 #if JXL_OS_HAIKU 28 #include <OS.h> 29 #endif // JXL_OS_HAIKU 30 31 namespace jxl { 32 33 double Now() { 34 #if JXL_OS_WIN 35 LARGE_INTEGER counter; 36 (void)QueryPerformanceCounter(&counter); 37 LARGE_INTEGER freq; 38 (void)QueryPerformanceFrequency(&freq); 39 return double(counter.QuadPart) / freq.QuadPart; 40 #elif JXL_OS_MAC 41 const auto t = mach_absolute_time(); 42 // On OSX/iOS platform the elapsed time is cpu time unit 43 // We have to query the time base information to convert it back 44 // See https://developer.apple.com/library/mac/qa/qa1398/_index.html 45 static mach_timebase_info_data_t timebase; 46 if (timebase.denom == 0) { 47 (void)mach_timebase_info(&timebase); 48 } 49 return double(t) * timebase.numer / timebase.denom * 1E-9; 50 #elif JXL_OS_HAIKU 51 return double(system_time_nsecs()) * 1E-9; 52 #else 53 timespec t; 54 clock_gettime(CLOCK_MONOTONIC, &t); 55 return t.tv_sec + t.tv_nsec * 1E-9; 56 #endif 57 } 58 59 } // namespace jxl