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
/**
* \file src/core/include/megbrain/utils/timer.h
* MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
*
* Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include <string>
#include "megbrain_build_config.h"
namespace mgb {
namespace timer_impl {
struct TimeSpec {
int64_t sec, nsec;
std::string to_string() const;
//! get time untile another time point
double time_until_secs(const TimeSpec& end) const {
// cast to int to avoid softfloat on 32bit platforms
return static_cast<int>(end.sec - sec) +
static_cast<int>(end.nsec - nsec) * 1e-9;
}
};
template <class TimerTrait>
class Timer {
TimeSpec m_start;
public:
MGE_WIN_DECLSPEC_FUC static TimeSpec get_time();
Timer() { reset(); }
void reset() { m_start = get_time(); }
double get_secs() const { return m_start.time_until_secs(get_time()); }
//! get milliseconds (one thousandth of a second)
double get_msecs() { return get_secs() * 1e3; }
//! get seconds and reset
double get_secs_reset() {
auto ret = get_secs();
reset();
return ret;
}
//! get milliseconds and reset
double get_msecs_reset() {
auto ret = get_msecs();
reset();
return ret;
}
};
class RealTimeTrait;
} // namespace timer_impl
using timer_impl::TimeSpec;
/*!
* \brief measure real time in nanoseconds precision
*/
using RealTimer = timer_impl::Timer<timer_impl::RealTimeTrait>;
} // namespace mgb
// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}