summaryrefslogtreecommitdiff
path: root/goo/GooTimer.cc
blob: 774ebbefb97f9e1a856236e0ce2d315fa5cacdf0 (plain)
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
//========================================================================
//
// GooTimer.cc
//
// Copyright 2005 Jonathan Blandford <jrb@redhat.com>
// Inspired by gtimer.c in glib, which is Copyright 2000 by the GLib Team
//
//========================================================================

#include <config.h>

#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif

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

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


GooTimer::GooTimer() {
  gettimeofday (&start, NULL);
  active = true;
}


void
GooTimer::stop() {
  gettimeofday (&end, NULL);
  active = false;
}

#define USEC_PER_SEC 1000000
double
GooTimer::getElapsed ()
{
  double total;
  struct timeval elapsed;

  if (active)
    gettimeofday (&end, NULL);

  if (start.tv_usec > end.tv_usec)
    {
      end.tv_usec += USEC_PER_SEC;
      end.tv_sec--;
    }

  elapsed.tv_usec = end.tv_usec - start.tv_usec;
  elapsed.tv_sec = end.tv_sec - start.tv_sec;

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

  return total;
}