summaryrefslogtreecommitdiff
path: root/timer.hpp
blob: 8d413b3828ad217c25613492a8282d9c3453396b (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
#pragma once

#include <sys/time.h>
#include <string>
#include <iostream>
#include <boost/optional.hpp>

struct PerfPrinter {
  timeval start;
  boost::optional<std::string> msg;

  PerfPrinter(boost::optional<std::string> msg) : msg(msg) {
    if (msg)
      std::cerr << "\r\033[K"
		<< *msg
		<< std::flush;
    gettimeofday(&start,NULL);
  }

  ~PerfPrinter() {
    if (msg)
      std::cerr << "\r\033[K"
		<< *msg
		<< ": "
		<< diff()
		<< " s"
		<< std::endl;
  }

  double diff() {
    timeval stop;
    gettimeofday(&stop,NULL);
    return (stop.tv_sec + stop.tv_usec/1000000.0) - (start.tv_sec + start.tv_usec/1000000.0);
  }
};
contact: Jan Huwald // Impressum