File: Timer.hh

package info (click to toggle)
exactimage 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,940 kB
  • sloc: cpp: 35,715; ansic: 1,730; xml: 1,447; makefile: 323; perl: 138; sh: 108; python: 45; php: 37; ruby: 12
file content (149 lines) | stat: -rw-r--r-- 3,350 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: utility/include/Timer.hh
 * General Sound Manipulation Program is Copyright (C) 2000 - 2008
 *   Valentin Ziegler and René Rebe
 * Copyright (C) 2015 - 2016, Ren\xc3\xa9 Rebe, ExactCODE GmbH
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */

/*
 * Alternatively, commercial licensing options are available from the
 * copyright holder ExactCODE GmbH Germany.
 */


/* Short Description:
 *   Some basic timer classes.
 */

#ifndef UTILITY__TIMER_HH__
#define UTILITY__TIMER_HH__

#ifndef _WIN32
#include <sys/time.h> // used by Timer
#include <sys/times.h> // used by TickTimer
#else
#include <sys/types.h>
#include <sys/timeb.h>
#endif

#include <stdint.h>

#include <iostream>
#include <string>

namespace Utility
{
  class Timer
  {
  public:
    Timer () { Reset (); }
    
    void Reset () { m_start = Value(); }
    uint64_t Delta () const { return Value() - m_start; }
    inline uint64_t PerSecond () const { return 1000000; }
    
    uint64_t Value () const {
#if !defined(_MSC_VER)
      timeval t_time;
      gettimeofday (&t_time, NULL);
      
      return (t_time.tv_sec * PerSecond()) + t_time.tv_usec;
#else
      struct timeb tb;
      ftime(&tb);
      return (uint64_t)tb.time * PerSecond() + tb.millitm * 1000;
#endif
    }
    
    const char* Unit () const { return "us"; }
  
  private:
    uint64_t m_start;
  };

#ifndef _WIN32
  class TickTimer
  {
  public:
    TickTimer ();
    
    void Reset ();
    uint64_t Delta () const;
    uint64_t PerSecond () const;
    
    uint64_t Value () const;
    
    const char* Unit () const { return "us"; }
    
  private:
    struct tms m_times;
  };
#endif

#if 0 /* dead code */

  class TimebaseTimer
  {
  public:
    TimebaseTimer ();
    
    void Reset ();
    uint64_t Delta () const;
    uint64_t PerSecond () const;
    
    uint64_t Value () const;

    const char* Unit () const { return "cycles"; }
    
  private:
    uint64_t start_tick;
  };

  template <typename T>
  class AutoTimer
  {
  public:
    AutoTimer (const std::string& i_text) {
      m_text = i_text;
    }
    ~AutoTimer () {
      std::cout << ">> AutoTimer: \"" << m_text
		<< "\" took: " << m_timer.Delta () << " " << m_timer.Unit () << std::endl;
    }
    
    uint64_t Delta () const {
      return m_timer.Delta ();
    }
    uint64_t PerSecond () const {
      return m_timer.PerSecond ();
    }
    
  private:
    T m_timer;
    std::string m_text;
  };

#endif
  
} // end namespace utility

#endif // UTILITY__TIMER_HH__