diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2016-09-07 18:22:41 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2016-09-07 18:22:41 +0300 |
| commit | 7a080b6d053fd0965641ee38dcd55fb7f015ebba (patch) | |
| tree | 2b2e8a784cc729649b4bf2107305b3b788980618 /timer.c | |
| download | breakout-7a080b6d053fd0965641ee38dcd55fb7f015ebba.tar.gz breakout-7a080b6d053fd0965641ee38dcd55fb7f015ebba.zip | |
Diffstat (limited to 'timer.c')
| -rw-r--r-- | timer.c | 48 |
1 files changed, 48 insertions, 0 deletions
@@ -0,0 +1,48 @@ +#include "timer.h" + +#include <time.h> + +#if defined(_WIN32) +#include <windows.h> +int64_t frequency; +#endif + +void TimerInit() +{ +#if defined(_WIN32) + QueryPerformanceFrequency((LARGE_INTEGER *) &frequency); +#endif +} + +int64_t TimerGetTimeMicro() +{ +#if defined(_WIN32) + int64_t value; + QueryPerformanceCounter((LARGE_INTEGER *) &value); + value = value * 1000LL*1000LL; + return value / frequency; +#else + struct timespec tp; + if (clock_gettime(CLOCK_MONOTONIC, &tp)) + { + return 0; + } + return (((int64_t) tp.tv_sec) * 1000LL*1000LL*1000LL + ((int64_t) tp.tv_nsec)) / 1000LL; +#endif +} + +void TimerStart(Timer *timer) +{ + *timer = TimerGetTimeMicro(); +} + +int64_t TimerElapsedMicro(Timer *timer) +{ + int64_t now = TimerGetTimeMicro(timer); + return now - *timer; +} + +int64_t TimerElapsedMilli(Timer *timer) +{ + return TimerElapsedMicro(timer) / 1000LL; +} |
