Scroom  0.14
timer.hh
Go to the documentation of this file.
1 /*
2  * Scroom - Generic viewer for 2D data
3  * Copyright (C) 2009-2022 Kees-Jan Dijkzeul
4  *
5  * SPDX-License-Identifier: LGPL-2.1
6  */
7 
8 #pragma once
9 
10 #include <string>
11 
12 #include <spdlog/spdlog.h>
13 #include <time.h>
14 
15 namespace Scroom
16 {
17  namespace Utils
18  {
19  class Timer
20  {
21  private:
22  struct timespec t;
23  std::string label;
24  bool valid;
25 
26  public:
27  Timer(std::string label)
28  : label(label)
29  {
30  valid = (0 == clock_gettime(CLOCK_MONOTONIC, &t));
31  }
32 
34  {
35  struct timespec t2;
36  bool v2 = (0 == clock_gettime(CLOCK_MONOTONIC, &t2));
37  if(valid && v2)
38  {
39  double elapsed = (t2.tv_nsec - t.tv_nsec) * 1e-9;
40  elapsed += t2.tv_sec - t.tv_sec;
41  spdlog::trace("{}: {:.9f}", label, elapsed);
42  }
43  else
44  {
45  spdlog::error("{}: Clock invalid", label);
46  }
47  }
48  };
49  } // namespace Utils
50 } // namespace Scroom
Scroom::Utils::Timer::label
std::string label
Definition: timer.hh:23
Scroom::Utils::Timer::t
struct timespec t
Definition: timer.hh:22
Scroom
Definition: assertions.hh:14
Scroom::Utils::Timer
Definition: timer.hh:19
Scroom::Utils::Timer::Timer
Timer(std::string label)
Definition: timer.hh:27
Scroom::Utils::Timer::valid
bool valid
Definition: timer.hh:24
Scroom::Utils::Timer::~Timer
~Timer()
Definition: timer.hh:33