Debian Codegraph @

src/c-blosc2/blosc/timestamp.c

raw
1 /*********************************************************************
2 Blosc - Blocked Shuffling and Compression Library
3
4 Copyright (c) 2021 Blosc Development Team <[email protected]>
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 #include <windows.h>
17
18 /* Set a timestamp value to the current time. */
19 void blosc_set_timestamp(blosc_timestamp_t* timestamp) {
20 /* Ignore the return value, assume the call always succeeds. */
21 QueryPerformanceCounter(timestamp);
22 }
23
24 /* Given two timestamp values, return the difference in nanoseconds. */
25 double blosc_elapsed_nsecs(blosc_timestamp_t start_time,
26 blosc_timestamp_t end_time) {
27 LARGE_INTEGER CounterFreq;
28 QueryPerformanceFrequency(&CounterFreq);
29
30 return (double)(end_time.QuadPart - start_time.QuadPart) /
31 ((double)CounterFreq.QuadPart / 1e9);
32 }
33
34 #else
35
36 #include <time.h>
37
38 #if defined(__MACH__) // OS X does not have clock_gettime, use clock_get_time
39
40 #include <mach/clock.h>
41
42 /* Set a timestamp value to the current time. */
43 void blosc_set_timestamp(blosc_timestamp_t* timestamp) {
44 clock_serv_t cclock;
45 mach_timespec_t mts;
46 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
47 clock_get_time(cclock, &mts);
48 mach_port_deallocate(mach_task_self(), cclock);
49 timestamp->tv_sec = mts.tv_sec;
50 timestamp->tv_nsec = mts.tv_nsec;
51 }
52
53 #else
54
55 /* Set a timestamp value to the current time. */
56 void blosc_set_timestamp(blosc_timestamp_t* timestamp) {
57 clock_gettime(CLOCK_MONOTONIC, timestamp);
58 }
59
60 #endif
61
62 /* Given two timestamp values, return the difference in nanoseconds. */
63 double blosc_elapsed_nsecs(blosc_timestamp_t start_time,
64 blosc_timestamp_t end_time) {
65 return (1e9 * (double)(end_time.tv_sec - start_time.tv_sec)) +
66 (double)(end_time.tv_nsec - start_time.tv_nsec);
67 }
68
69 #endif
70
71 /* Given two timeval stamps, return the difference in seconds */
72 double blosc_elapsed_secs(blosc_timestamp_t last, blosc_timestamp_t current) {
73 return 1e-9 * blosc_elapsed_nsecs(last, current);
74 }