File: ScopedDebugTimer.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (121 lines) | stat: -rw-r--r-- 2,350 bytes parent folder | download | duplicates (3)
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
#pragma once

#include "itextstream.h"

#if defined(_MSC_VER) || defined(_WINDOWS_)
   #include <time.h>
   #include <windows.h>

	#undef min
	#undef max

   #if !defined(_WINSOCK2API_) && !defined(_WINSOCKAPI_)
         struct timeval
         {
            long tv_sec;
            long tv_usec;
         };
   #endif
#else
   #include <sys/time.h>
#endif

#if defined(_MSC_VER) || defined(_WINDOWS_)
   inline int gettimeofday(struct timeval* tv, void*)
   {
      union {
         long long ns100;
         FILETIME ft;
      } now;

      GetSystemTimeAsFileTime (&now.ft);
      tv->tv_usec = (long) ((now.ns100 / 10LL) % 1000000LL);
      tv->tv_sec = (long) ((now.ns100 - 116444736000000000LL) / 10000000LL);
     return (0);
   }
#endif

#include <string>

namespace {

	const double MILLION = 1000000.0;

	/**
	 * Operator- for timeval structures.
	 *
	 * @returns
	 * Double-precision float representing the difference in seconds between
	 * the two times.
	 */
	double operator-(const timeval& l, const timeval& r) {

		// Convert timevals to double
		double dl = (double) l.tv_sec + ((double) l.tv_usec / MILLION);
		double dr = (double) r.tv_sec + ((double) r.tv_usec / MILLION);

		return dl - dr;
	}
}

/**
 * Debugging class to time a particular event. The clock is saved during
 * construction, and the time difference calculated at destruction.
 */
class ScopedDebugTimer
{
private:

	// Start time
	timeval _s;

	// Name of operation
	std::string _op;

	// Show FPS?
	bool _fps;

public:

	/**
	 * Constructor. Set the name of the operation to be printed out on
	 * destruction.
	 *
	 * @param name
	 * The name of the operation.
	 *
	 * @param showFps
	 * If true, a nominal FPS value will be calculated for the given operation
	 * time.
	 */
	ScopedDebugTimer(const std::string& name, bool showFps = false)
	: _op(name), _fps(showFps)
	{
		// Save start time
		gettimeofday(&_s, nullptr);
	}

	/**
	 * Destructor. Prints out the time of the operation.
	 */
	~ScopedDebugTimer()
    {
		// Get the current time
		timeval end;
		gettimeofday(&end, nullptr);

		// Calculate duration
		double duration = end - _s;

        auto stream = rMessage();

        stream << _op << " in " << duration << " seconds";

		if (_fps)
        {
            stream << " (" << (1.0 / duration) << " FPS)";
		}

        stream << std::endl;
	}
};