aboutsummaryrefslogtreecommitdiffstats
path: root/checks/timer.cpp
blob: 4abd8a05f3bff8873e2cad40e3089fa564d358f9 (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
#include "timer.h"
#include <time.h>
#include <iomanip>

u64bit Timer::get_clock()
   {
   struct timespec tv;
   clock_gettime(CLOCK_REALTIME, &tv);
   return (tv.tv_sec * 1000000000ULL + tv.tv_nsec);
   }

Timer::Timer(const std::string& n) : name(n)
   {
   time_used = 0;
   timer_start = 0;
   event_count = 0;
   }

void Timer::start()
   {
   stop();
   timer_start = get_clock();
   }

void Timer::stop()
   {
   if(timer_start)
      {
      u64bit now = get_clock();

      if(now > timer_start)
         time_used += (now - timer_start);

      timer_start = 0;
      ++event_count;
      }
   }

std::ostream& operator<<(std::ostream& out, Timer& timer)
   {
   //out << timer.value() << " ";

   int events_per_second = timer.events() / timer.seconds();

   out << events_per_second << " " << timer.get_name() << " per second; ";

   if(timer.seconds_per_event() < 10)
      out << std::setprecision(2) << std::fixed
          << timer.ms_per_event() << " ms/" << timer.get_name();
   else
      out << std::setprecision(4) << std::fixed
          << timer.seconds_per_event() << " s/" << timer.get_name();

   if(timer.seconds() < 10)
      out << " (" << timer.events() << " ops in "
          << timer.milliseconds() << " ms)";
   else
      out << " (" << timer.events() << " ops in "
          << timer.seconds() << " s)";

   return out;
   }