1 /*********************************************************************
2   Blosc - Blocked Shuffling and Compression Library
3 
4   Copyright (C) 2021  The Blosc Developers <blosc@blosc.org>
5   https://blosc.org
6   License: BSD 3-Clause (see LICENSE.txt)
7 
8   See LICENSE.txt for details about copyright and rights to use.
9 **********************************************************************/
10 
11 #include "blosc2.h"
12 
13 /* System-specific high-precision timing functions. */
14 #if defined(_WIN32)
15 
16 /* Set a timestamp value to the current time. */
blosc_set_timestamp(blosc_timestamp_t * timestamp)17 void blosc_set_timestamp(blosc_timestamp_t* timestamp) {
18   /* Ignore the return value, assume the call always succeeds. */
19   QueryPerformanceCounter(timestamp);
20 }
21 
22 /* Given two timestamp values, return the difference in nanoseconds. */
blosc_elapsed_nsecs(blosc_timestamp_t start_time,blosc_timestamp_t end_time)23 double blosc_elapsed_nsecs(blosc_timestamp_t start_time,
24                            blosc_timestamp_t end_time) {
25   LARGE_INTEGER CounterFreq;
26   QueryPerformanceFrequency(&CounterFreq);
27 
28   return (double)(end_time.QuadPart - start_time.QuadPart) /
29     ((double)CounterFreq.QuadPart / 1e9);
30 }
31 
32 #else
33 
34 /* Set a timestamp value to the current time. */
blosc_set_timestamp(blosc_timestamp_t * timestamp)35 void blosc_set_timestamp(blosc_timestamp_t* timestamp) {
36 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
37   clock_serv_t cclock;
38   mach_timespec_t mts;
39   host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
40   clock_get_time(cclock, &mts);
41   mach_port_deallocate(mach_task_self(), cclock);
42   timestamp->tv_sec = mts.tv_sec;
43   timestamp->tv_nsec = mts.tv_nsec;
44 #else
45   clock_gettime(CLOCK_MONOTONIC, timestamp);
46 #endif
47 }
48 
49 /* Given two timestamp values, return the difference in nanoseconds. */
blosc_elapsed_nsecs(blosc_timestamp_t start_time,blosc_timestamp_t end_time)50 double blosc_elapsed_nsecs(blosc_timestamp_t start_time,
51                            blosc_timestamp_t end_time) {
52   return (1e9 * (end_time.tv_sec - start_time.tv_sec)) +
53           (end_time.tv_nsec - start_time.tv_nsec);
54 }
55 
56 #endif
57 
58 /* Given two timeval stamps, return the difference in seconds */
blosc_elapsed_secs(blosc_timestamp_t last,blosc_timestamp_t current)59 double blosc_elapsed_secs(blosc_timestamp_t last, blosc_timestamp_t current) {
60   return 1e-9 * blosc_elapsed_nsecs(last, current);
61 }
62