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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "libvideostitch/profile.hpp"
#include "libvideostitch/logging.hpp"
#include <iostream>
#include <sstream>
namespace VideoStitch {
namespace Util {
SimpleProfiler::SimpleProfiler(const char* title, bool usecs, ThreadSafeOstream& out)
: _out(out), _title(title), usecs(usecs) {
resetTimer();
}
SimpleProfiler::~SimpleProfiler() {
const uint64_t duration = computeDuration();
std::stringstream msg;
msg << _title << ": ";
if (usecs) {
msg << duration << " usec" << std::endl;
} else {
msg << duration / 1000 << " ms" << std::endl;
}
_out << msg.str();
}
void SimpleProfiler::resetTimer() {
#ifdef _MSC_VER
QueryPerformanceCounter(&_tv);
#else
gettimeofday(&_tv, NULL);
#endif
}
uint64_t SimpleProfiler::computeDuration() {
#ifdef _MSC_VER
LARGE_INTEGER stop;
QueryPerformanceCounter(&stop);
LARGE_INTEGER countsPerSecond;
QueryPerformanceFrequency(&countsPerSecond);
uint64_t duration = (uint64_t)((1000000 * (stop.QuadPart - _tv.QuadPart)) / countsPerSecond.QuadPart);
#else
struct timeval stop;
gettimeofday(&stop, NULL);
uint64_t duration = ((uint64_t)stop.tv_sec * 1000000 + stop.tv_usec) - ((uint64_t)_tv.tv_sec * 1000000 + _tv.tv_usec);
#endif
return duration;
}
SimpleTimer::SimpleTimer() : SimpleProfiler("", true, Logger::get(Logger::Info)) { // dummy parameters
}
void SimpleTimer::reset() { return resetTimer(); }
uint64_t SimpleTimer::elapsed() { return computeDuration(); }
} // namespace Util
} // namespace VideoStitch