1//========================================================================
2//
3// GooTimer.cc
4//
5// This file is licensed under GPLv2 or later
6//
7// Copyright 2005 Jonathan Blandford <jrb@redhat.com>
8// Copyright 2007 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
9// Copyright 2010 Hib Eris <hib@hiberis.nl>
10// Inspired by gtimer.c in glib, which is Copyright 2000 by the GLib Team
11//
12//========================================================================
13
14#include <config.h>
15
16#include "GooTimer.h"
17#include <cstring>
18
19#define USEC_PER_SEC 1000000
20
21//------------------------------------------------------------------------
22// GooTimer
23//------------------------------------------------------------------------
24
25GooTimer::GooTimer()
26{
27 start();
28}
29
30void GooTimer::start()
31{
32#ifdef HAVE_GETTIMEOFDAY
33 gettimeofday(tv: &start_time, tz: nullptr);
34#elif defined(_WIN32)
35 QueryPerformanceCounter(&start_time);
36#endif
37 active = true;
38}
39
40void GooTimer::stop()
41{
42#ifdef HAVE_GETTIMEOFDAY
43 gettimeofday(tv: &end_time, tz: nullptr);
44#elif defined(_WIN32)
45 QueryPerformanceCounter(&end_time);
46#endif
47 active = false;
48}
49
50#ifdef HAVE_GETTIMEOFDAY
51double GooTimer::getElapsed()
52{
53 double total;
54 struct timeval elapsed;
55
56 if (active) {
57 gettimeofday(tv: &end_time, tz: nullptr);
58 }
59
60 if (start_time.tv_usec > end_time.tv_usec) {
61 end_time.tv_usec += USEC_PER_SEC;
62 end_time.tv_sec--;
63 }
64
65 elapsed.tv_usec = end_time.tv_usec - start_time.tv_usec;
66 elapsed.tv_sec = end_time.tv_sec - start_time.tv_sec;
67
68 total = elapsed.tv_sec + ((double)elapsed.tv_usec / 1e6);
69 if (total < 0) {
70 total = 0;
71 }
72
73 return total;
74}
75#elif defined(_WIN32)
76double GooTimer::getElapsed()
77{
78 LARGE_INTEGER freq;
79 double time_in_secs;
80 QueryPerformanceFrequency(&freq);
81
82 if (active)
83 QueryPerformanceCounter(&end_time);
84
85 time_in_secs = (double)(end_time.QuadPart - start_time.QuadPart) / (double)freq.QuadPart;
86 return time_in_secs * 1000.0;
87}
88#else
89double GooTimer::getElapsed()
90{
91# warning "no support for GooTimer"
92 return 0;
93}
94#endif
95

source code of poppler/goo/GooTimer.cc