1 Star 1 Fork 0

mirror/spdlog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
stopwatch.h 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
Gabi Melman 提交于 3年前 . Fix #2434
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/fmt/fmt.h>
#include <chrono>
// Stopwatch support for spdlog (using std::chrono::steady_clock).
// Displays elapsed seconds since construction as double.
//
// Usage:
//
// spdlog::stopwatch sw;
// ...
// spdlog::debug("Elapsed: {} seconds", sw); => "Elapsed 0.005116733 seconds"
// spdlog::info("Elapsed: {:.6} seconds", sw); => "Elapsed 0.005163 seconds"
//
//
// If other units are needed (e.g. millis instead of double), include "fmt/chrono.h" and use "duration_cast<..>(sw.elapsed())":
//
// #include <spdlog/fmt/chrono.h>
//..
// using std::chrono::duration_cast;
// using std::chrono::milliseconds;
// spdlog::info("Elapsed {}", duration_cast<milliseconds>(sw.elapsed())); => "Elapsed 5ms"
namespace spdlog {
class stopwatch
{
using clock = std::chrono::steady_clock;
std::chrono::time_point<clock> start_tp_;
public:
stopwatch()
: start_tp_{clock::now()}
{}
std::chrono::duration<double> elapsed() const
{
return std::chrono::duration<double>(clock::now() - start_tp_);
}
void reset()
{
start_tp_ = clock::now();
}
};
} // namespace spdlog
// Support for fmt formatting (e.g. "{:012.9}" or just "{}")
namespace
#ifdef SPDLOG_USE_STD_FORMAT
std
#else
fmt
#endif
{
template<>
struct formatter<spdlog::stopwatch> : formatter<double>
{
template<typename FormatContext>
auto format(const spdlog::stopwatch &sw, FormatContext &ctx) -> decltype(ctx.out())
{
return formatter<double>::format(sw.elapsed().count(), ctx);
}
};
} // namespace std
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirror_1/spdlog.git
git@gitee.com:mirror_1/spdlog.git
mirror_1
spdlog
spdlog
v1.x

搜索帮助