summaryrefslogtreecommitdiff
path: root/gi/pyp-topics/src/timing.h
blob: 7543295c3ecd64afae001f2156d7b9f3d0dea034 (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
#ifndef TIMING_H
#define TIMING_H

#ifdef __CYGWIN__
# ifndef _POSIX_MONOTONIC_CLOCK
#  define _POSIX_MONOTONIC_CLOCK
# endif
#endif

#include <time.h>
#include <sys/time.h>
#include "clock_gettime_stub.c"

struct Timer {
  Timer() { Reset(); }
  void Reset()
  {
    clock_gettime(CLOCK_MONOTONIC, &start_t);
  }
  double Elapsed() const {
    timespec end_t;
    clock_gettime(CLOCK_MONOTONIC, &end_t);
    const double elapsed = (end_t.tv_sec - start_t.tv_sec)
                + (end_t.tv_nsec - start_t.tv_nsec) / 1000000000.0;
    return elapsed;
  }
 private:
  timespec start_t;
};

#endif