-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtinytime.cc
More file actions
34 lines (33 loc) · 1.01 KB
/
tinytime.cc
File metadata and controls
34 lines (33 loc) · 1.01 KB
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
// Tiny timing utilities. rlyeh, public domain | wtrmrkrlyeh
#include <thread>
#include <chrono>
#if !defined(TIMING_USE_OMP) && ( defined(USE_OMP) || defined(_MSC_VER) /*|| defined(__ANDROID_API__)*/ )
# define TIMING_USE_OMP
# include <omp.h>
#endif
double now() {
# ifdef TIMING_USE_OMP
static auto const epoch = omp_get_wtime();
return omp_get_wtime() - epoch;
# else
static auto const epoch = std::chrono::steady_clock::now(); // milli ms > micro us > nano ns
return std::chrono::duration_cast< std::chrono::microseconds >( std::chrono::steady_clock::now() - epoch ).count() / 1000000.0;
# endif
}
double bench( void (*fn)() ) {
double took = -now();
return ( fn(), took + now() );
}
void sleep( double secs ) {
std::chrono::microseconds duration( (int)(secs * 1000000) );
std::this_thread::sleep_for( duration );
}
/*
#include <stdio.h>
int main() {
double t0 = now();
printf("%g s.\n", bench( []{ sleep(0.1234); } ) );
double t1 = now();
printf("%g s.\n", t1 - t0 );
}
*/