spdlog
Loading...
Searching...
No Matches
include
spdlog
stopwatch.h
Go to the documentation of this file.
1
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4
#pragma once
5
6
#include <
spdlog/fmt/fmt.h
>
7
8
// Stopwatch support for spdlog (using std::chrono::steady_clock).
9
// Displays elapsed seconds since construction as double.
10
//
11
// Usage:
12
//
13
// spdlog::stopwatch sw;
14
// ...
15
// spdlog::debug("Elapsed: {} seconds", sw); => "Elapsed 0.005116733 seconds"
16
// spdlog::info("Elapsed: {:.6} seconds", sw); => "Elapsed 0.005163 seconds"
17
//
18
//
19
// If other units are needed (e.g. millis instead of double), include "fmt/chrono.h" and use "duration_cast<..>(sw.elapsed())":
20
//
21
// #include <spdlog/fmt/chrono.h>
22
//..
23
// using std::chrono::duration_cast;
24
// using std::chrono::milliseconds;
25
// spdlog::info("Elapsed {}", duration_cast<milliseconds>(sw.elapsed())); => "Elapsed 5ms"
26
27
namespace
spdlog
{
28
class
stopwatch
29
{
30
using
clock
=
std::chrono::steady_clock
;
31
std::chrono::time_point<clock>
start_tp_
;
32
33
public
:
34
stopwatch
()
35
:
start_tp_
{
clock
::now()}
36
{}
37
38
std::chrono::duration<double>
elapsed
()
const
39
{
40
return
std::chrono::duration<double>
(
clock::now
() -
start_tp_
);
41
}
42
43
void
reset
()
44
{
45
start_tp_
= clock ::now();
46
}
47
};
48
}
// namespace spdlog
49
50
// Support for fmt formatting (e.g. "{:012.9}" or just "{}")
51
namespace
fmt
{
52
template
<>
53
struct
formatter
<
spdlog
::stopwatch> :
formatter
<double>
54
{
55
template
<
typename
FormatContext>
56
auto
format
(
const
spdlog::stopwatch
&sw, FormatContext &ctx) ->
decltype
(ctx.out())
57
{
58
return
formatter<double>::format
(sw.elapsed().count(), ctx);
59
}
60
};
61
}
// namespace fmt
spdlog::stopwatch
Definition
stopwatch.h:29
spdlog::stopwatch::stopwatch
stopwatch()
Definition
stopwatch.h:34
spdlog::stopwatch::start_tp_
std::chrono::time_point< clock > start_tp_
Definition
stopwatch.h:31
spdlog::stopwatch::reset
void reset()
Definition
stopwatch.h:43
spdlog::stopwatch::elapsed
std::chrono::duration< double > elapsed() const
Definition
stopwatch.h:38
std::chrono::duration
fmt.h
fmt
Definition
bin_to_hex.h:79
spdlog
Definition
async.h:25
std::chrono::steady_clock::now
T now(T... args)
std::chrono::steady_clock
fmt::formatter< spdlog::stopwatch >::format
auto format(const spdlog::stopwatch &sw, FormatContext &ctx) -> decltype(ctx.out())
Definition
stopwatch.h:56
formatter
Definition
core.h:694
std::chrono::time_point
Generated by
1.9.8