File: GooTimer.cc

package info (click to toggle)
poppler 20.09.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 16,092 kB
  • sloc: cpp: 151,577; ansic: 30,155; python: 367; makefile: 42; sh: 36
file content (92 lines) | stat: -rw-r--r-- 2,076 bytes parent folder | download | duplicates (2)
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
//========================================================================
//
// GooTimer.cc
//
// This file is licensed under GPLv2 or later
//
// Copyright 2005 Jonathan Blandford <jrb@redhat.com>
// Copyright 2007 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
// Copyright 2010 Hib Eris <hib@hiberis.nl>
// Inspired by gtimer.c in glib, which is Copyright 2000 by the GLib Team
//
//========================================================================

#include <config.h>

#include "GooTimer.h"
#include <cstring>

#define USEC_PER_SEC 1000000

//------------------------------------------------------------------------
// GooTimer
//------------------------------------------------------------------------

GooTimer::GooTimer()
{
    start();
}

void GooTimer::start()
{
#ifdef HAVE_GETTIMEOFDAY
    gettimeofday(&start_time, nullptr);
#elif defined(_WIN32)
    QueryPerformanceCounter(&start_time);
#endif
    active = true;
}

void GooTimer::stop()
{
#ifdef HAVE_GETTIMEOFDAY
    gettimeofday(&end_time, nullptr);
#elif defined(_WIN32)
    QueryPerformanceCounter(&end_time);
#endif
    active = false;
}

#ifdef HAVE_GETTIMEOFDAY
double GooTimer::getElapsed()
{
    double total;
    struct timeval elapsed;

    if (active)
        gettimeofday(&end_time, nullptr);

    if (start_time.tv_usec > end_time.tv_usec) {
        end_time.tv_usec += USEC_PER_SEC;
        end_time.tv_sec--;
    }

    elapsed.tv_usec = end_time.tv_usec - start_time.tv_usec;
    elapsed.tv_sec = end_time.tv_sec - start_time.tv_sec;

    total = elapsed.tv_sec + ((double)elapsed.tv_usec / 1e6);
    if (total < 0)
        total = 0;

    return total;
}
#elif defined(_WIN32)
double GooTimer::getElapsed()
{
    LARGE_INTEGER freq;
    double time_in_secs;
    QueryPerformanceFrequency(&freq);

    if (active)
        QueryPerformanceCounter(&end_time);

    time_in_secs = (double)(end_time.QuadPart - start_time.QuadPart) / (double)freq.QuadPart;
    return time_in_secs * 1000.0;
}
#else
double GooTimer::getElapsed()
{
#    warning "no support for GooTimer"
    return 0;
}
#endif