-
Notifications
You must be signed in to change notification settings - Fork 23
/
timerc
109 lines (101 loc) · 3.96 KB
/
timerc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/****************************************************************************
* ArtraCFD *
* <By Huangrui Mo> *
* Copyright (C) Huangrui Mo <[email protected]> *
* This file is part of ArtraCFD. *
* ArtraCFD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
****************************************************************************/
/****************************************************************************
* Required Header Files
****************************************************************************/
#include "timer.h"
#if defined(_WIN32)
#include <Windows.h>
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
/* enable POSIX definitions according to std version */
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600 /* 600: POSIX 2004; 700: POSIX 2008 */
#else
#define _XOPEN_SOURCE 500 /* 500: POSIX 1995 */
#endif
#include <unistd.h> /* enable POSIX flags */
#include <time.h> /* enable clock_gettime(), time() */
#include <sys/time.h> /* enable gethrtime(), gettimeofday() */
#if defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>
#include <mach/mach_time.h>
#endif
#else
#error "cannot define timer for an unknown OS..."
#endif
/****************************************************************************
* Function definitions
****************************************************************************/
/*
* Adapted from getRealTime.c by David Robert Nadeau
*/
double GetTime(void)
{
#if defined(_WIN32)
FILETIME tm;
ULONGLONG t;
#if defined(NTDDI_WIN8) && NTDDI_VERSION >= NTDDI_WIN8
/* Windows 8, Windows Server 2012 and later */
GetSystemTimePreciseAsFileTime(&tm);
#else
/* Windows 2000 and later */
GetSystemTimeAsFileTime(&tm);
#endif
t = ((ULONGLONG)(tm.dwHighDateTime) << 32) | (ULONGLONG)(tm.dwLowDateTime);
return (double)(t) / 10000000.0;
#elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__)))
/* HP-UX, Solaris */
return (double)(gethrtime()) / 1000000000.0;
#elif defined(__APPLE__) && defined(__MACH__)
/* OSX */
mach_timebase_info_data_t timeBase;
(void)mach_timebase_info(&timeBase);
return ((double)(timeBase.numer) / (double)(timeBase.denom)) *
((double)(mach_absolute_time()) / 1000000000.0);
#elif defined(_POSIX_VERSION)
/* POSIX */
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
{
struct timespec ts;
#if defined(CLOCK_MONOTONIC_PRECISE)
/* BSD */
const clockid_t id = CLOCK_MONOTONIC_PRECISE;
#elif defined(CLOCK_MONOTONIC_RAW)
/* Linux */
const clockid_t id = CLOCK_MONOTONIC_RAW;
#elif defined(CLOCK_HIGHRES)
/* Solaris */
const clockid_t id = CLOCK_HIGHRES;
#elif defined(CLOCK_MONOTONIC)
/* AIX, BSD, Linux, POSIX, Solaris */
const clockid_t id = CLOCK_MONOTONIC;
#elif defined(CLOCK_REALTIME)
/* AIX, BSD, HP-UX, Linux, POSIX */
const clockid_t id = CLOCK_REALTIME;
#else
/* Unknown */
const clockid_t id = (clockid_t) - 1;
#endif
if ((id != (clockid_t) - 1) && (clock_gettime(id, &ts) != -1)) {
return (double)(ts.tv_sec) + (double)(ts.tv_nsec) / 1000000000.0;
}
/* Fall through */
}
#endif
/* AIX, BSD, Cygwin, HP-UX, Linux, OSX, POSIX, Solaris */
struct timeval tm;
gettimeofday(&tm, NULL);
return (double)(tm.tv_sec) + (double)(tm.tv_usec) / 1000000.0;
#else
return -1.0; /* Failed */
#endif
}
/* a good practice: end file with a newline */