本来想自己写的,一看github上面都有就不再重复造轮子了。github上的项目如下:

  • StopWatch 纯标准库实现:使用std::chrono::high_resolution_clock,其实就是std::chrono::steady_clock的别名。
  • StopWatch 类似C#的实现:和C#的StopWatch比较像,在Windows下使用的是QueryPerformanceCounter系统API,其它系统下使用std::chrono::steady_clock

纯标准库实现

第一种纯标准库实现的Stopwatch.hpp内容如下:

// Copyright Ingo Proff 2017.
// https://github.com/CrikeeIP/Stopwatch
// Distributed under the MIT Software License (X11 license).
// (See accompanying file LICENSE) #pragma once #include <vector>
#include <string>
#include <chrono> namespace stopwatch{ class Stopwatch{
public:
enum TimeFormat{ NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS }; Stopwatch(): start_time(), laps({}) {
start();
} void start(){
start_time = std::chrono::high_resolution_clock::now();
laps = {start_time};
} template<TimeFormat fmt = TimeFormat::MILLISECONDS>
std::uint64_t lap(){
const auto t = std::chrono::high_resolution_clock::now();
const auto last_r = laps.back();
laps.push_back( t );
return ticks<fmt>(last_r, t);
} template<TimeFormat fmt = TimeFormat::MILLISECONDS>
std::uint64_t elapsed(){
const auto end_time = std::chrono::high_resolution_clock::now();
return ticks<fmt>(start_time, end_time);
} template<TimeFormat fmt_total = TimeFormat::MILLISECONDS, TimeFormat fmt_lap = fmt_total>
std::pair<std::uint64_t, std::vector<std::uint64_t>> elapsed_laps(){
std::vector<std::uint64_t> lap_times;
lap_times.reserve(laps.size()-1); for( std::size_t idx = 0; idx <= laps.size()-2; idx++){
const auto lap_end = laps[idx+1];
const auto lap_start = laps[idx];
lap_times.push_back( ticks<fmt_lap>(lap_start, lap_end) );
} return { ticks<fmt_total>(start_time, laps.back()), lap_times };
} private:
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_pt;
time_pt start_time;
std::vector<time_pt> laps; template<TimeFormat fmt = TimeFormat::MILLISECONDS>
static std::uint64_t ticks( const time_pt& start_time, const time_pt& end_time){
const auto duration = end_time - start_time;
const std::uint64_t ns_count = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count(); switch(fmt){
case TimeFormat::NANOSECONDS:
{
return ns_count;
}
case TimeFormat::MICROSECONDS:
{
std::uint64_t up = ((ns_count/100)%10 >= 5) ? 1 : 0;
const auto mus_count = (ns_count /1000) + up;
return mus_count;
}
case TimeFormat::MILLISECONDS:
{
std::uint64_t up = ((ns_count/100000)%10 >= 5) ? 1 : 0;
const auto ms_count = (ns_count /1000000) + up;
return ms_count;
}
case TimeFormat::SECONDS:
{
std::uint64_t up = ((ns_count/100000000)%10 >= 5) ? 1 : 0;
const auto s_count = (ns_count /1000000000) + up;
return s_count;
}
}
}
}; constexpr Stopwatch::TimeFormat ns = Stopwatch::TimeFormat::NANOSECONDS;
constexpr Stopwatch::TimeFormat mus = Stopwatch::TimeFormat::MICROSECONDS;
constexpr Stopwatch::TimeFormat ms = Stopwatch::TimeFormat::MILLISECONDS;
constexpr Stopwatch::TimeFormat s = Stopwatch::TimeFormat::SECONDS; constexpr Stopwatch::TimeFormat nanoseconds = Stopwatch::TimeFormat::NANOSECONDS;
constexpr Stopwatch::TimeFormat microseconds = Stopwatch::TimeFormat::MICROSECONDS;
constexpr Stopwatch::TimeFormat milliseconds = Stopwatch::TimeFormat::MILLISECONDS;
constexpr Stopwatch::TimeFormat seconds = Stopwatch::TimeFormat::SECONDS; std::string show_times( const std::vector<std::uint64_t>& times ){
std::string result("{");
for( const auto& t : times ){
result += std::to_string(t) + ",";
}
result.back() = static_cast<char>('}');
return result;
} }

使用示例如下:

//创建一个stopwatch
sw::Stopwatch my_watch;
my_watch.start(); //Do something time-consuming here... //纳秒
std::uint64_t elapsed_ns = my_watch.elapsed<sw::ns>();
//微秒
std::uint64_t elapsed_mus = my_watch.elapsed<sw::mus>();
//毫秒
std::uint64_t elapsed_ms = my_watch.elapsed();
//秒
std::uint64_t elapsed_s = my_watch.elapsed<sw::s>();

类似C#的实现

第二种类似C#的实现,StopWatch.h代码如下:

#ifndef __STOPWATCH_H__
#define __STOPWATCH_H__ #if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
#include <Windows.h>
#else
#include <chrono>
#endif class StopWatch
{
public:
StopWatch();
~StopWatch(); //开启计时
void Start(); //暂停计时
void Stop(); //重新计时
void ReStart(); //微秒
double Elapsed(); //毫秒
double ElapsedMS(); //秒
double ElapsedSecond(); private:
long long elapsed_;
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
LARGE_INTEGER start_;
LARGE_INTEGER stop_;
LARGE_INTEGER frequency_;
#else
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::microseconds MicroSeconds;
std::chrono::steady_clock::time_point start_;
std::chrono::steady_clock::time_point stop_;
#endif }; #endif // __STOPWATCH_H__

StopWatch.cpp代码如下:

#include "StopWatch.h"

#if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
StopWatch::StopWatch():elapsed_(0)
{
elapsed_ = 0;
start_.QuadPart = 0;
stop_.QuadPart = 0;
QueryPerformanceFrequency(&frequency_);
}
#else
StopWatch::StopWatch():elapsed_(0),start_(MicroSeconds::zero()),stop_(MicroSeconds::zero())
{
}
#endif StopWatch::~StopWatch()
{
} void StopWatch::Start()
{
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
QueryPerformanceCounter(&start_);
#else
start_ = Clock::now();
#endif } void StopWatch::Stop()
{
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(WIN32)
QueryPerformanceCounter(&stop_);
elapsed_ += (stop_.QuadPart - start_.QuadPart) * 1000000 / frequency_.QuadPart;
#else
stop_ = Clock::now();
elapsed_ = std::chrono::duration_cast<MicroSeconds>(stop_ - start_).count();
#endif } void StopWatch::ReStart()
{
elapsed_ = 0;
Start();
} double StopWatch::Elapsed()
{
return static_cast<double>(elapsed_);
} double StopWatch::ElapsedMS()
{
return elapsed_ / 1000.0;
} double StopWatch::ElapsedSecond()
{
return elapsed_ / 1000000.0;
}

使用示例如下(和C#比较像):

StopWatch sw;
sw.Start();
//Do something time-consuming here...
sw.Stop();
std::cout << "运行时间:" << sw.ElapsedMS() << "毫秒" << std::endl;

总结

  • 如果有代码洁癖的话就使用第一种,纯标准库实现、功能全面、使用方法偏向传统C++。
  • 如果不介意使用系统API的话就使用第二种,功能简单、使用方法偏向传统C#。

C++统计代码运行时间的更多相关文章

  1. c#实现统计代码运行时间

    方法一: //实例化一个计时器 Stopwatch watch = new Stopwatch(); //開始计时 watch.Start(); //此处为要计算的执行代码 for (int i = ...

  2. C++统计程序运行时间代码片段

    因为经常需要统计代码的运行时间,所以计时功能就显得很重要, 记录一下现在喜欢用的计时方式,供日后查阅. 1.下面是计时主函数, bool TimeStaticMine(int id,const cha ...

  3. 在 Linux 如何优雅的统计程序运行时间?恕我直言,你运行的可能是假 time

    最近在使用 time 命令时,无意间发现了一些隐藏的小秘密和强大功能,今天分享给大家. time 在 Linux 下是比较常用的命令,可以帮助我们方便的计算程序的运行时间,对比采用不同方案时程序的运行 ...

  4. C#如何测试代码运行时间

    1.System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 开始监视代码运行时间 // 需要测试 ...

  5. VS2010统计代码行数 [转]

    按CTRL+SHIFT+F (Find in files),勾上支持正则表达式,然后输入搜索内容:  ^:b*[^:b#/]+.*$ 以上表达式的统计可做到:#开头 和 /开头 或者 空行 都不计入代 ...

  6. Eclipse统计代码行数

    开发过程中,经常需要统计代码行数,这时可以通过Eclipse的Search功能来实现. 步骤: 1.在Package Explorer中选中需要统计的包: 2.单击菜单Search-->File ...

  7. Google Analytics统计代码GA.JS中文教程

    2010-12-06 11:07:08|  分类: java编程 |  标签:google  analytics  ga  js  代码  |举报|字号 订阅     Google Analytics ...

  8. Visual Studio VS2010统计代码行数(转载)

    本文转自:http://blog.csdn.net/zhouworld16/article/details/9292851 在网上看到别人用的方法: 按CTRL+SHIFT+F (Find in fi ...

  9. 使用console进行 性能测试 和 计算代码运行时间(转载)

    本文转载自: 使用console进行 性能测试 和 计算代码运行时间

  10. 如何给WordPress安装百度统计代码

    1.注册并登录百度统计,点击页面顶部的“网站中心”,然后点击右上角“+ 新增网站”,填写网站域名确定后,点击“复制代码”:2.登录 WordPress 后台,点击左侧导航栏“外观”里的“编辑”,然后点 ...

随机推荐

  1. Linux系统的一些实用操作 [补档-2023-07-30]

    Linux的实用操作 4-1.常用快捷键 强制停止:当某些程序运行时,或者命令输入错误时,可以通过 ctrl + c 来强制结束当前的操作. 退出或登出:当我们要退出某些用户时,或者要退出某些特殊的页 ...

  2. nodejs连接mysql报错:throw err; // Rethrow non-MySQL errors TypeError: Cannot read property 'query' of undefined

    该问题的解决方案如下: win+R 输入cmd mysql -u root -p 输入密码进入到mysql 3.执行sql语句,将密码改成123456(自己可以记住的密码即可) alter user ...

  3. 记录开发中element树形控件数据应用在页面上的相关问题

    业务场景 根据后台返回数据生成角色权限的树形结构.获取节点数据后,当父节点被勾选时,所有的子节点全部被勾选,而实际上后台并没有返回当前父节点的所有子节点的ID,所以应该只有部分子节点被勾选. 下面第一 ...

  4. 优化算法之梯度下降|Matlab实现梯度下降算法

    题目要求: 使用Matab实现梯度下降法 对于函数: min ⁡ f ( x ) = 2 x 1 2 + 4 x 2 2 − 6 x 1 − 2 x 1 x 2 \min f(x)=2 x_{1}^{ ...

  5. macOS 上 常用的操作

    首先 mac上 若使用的是windows的键盘,那么需要把ctrl 键,设置成 cmd键,因为mac上大多数操作都是 基于cmd键. 1.将ctrl键,修改为cmd键,这样后 复制.粘贴.剪切.全选等 ...

  6. golang 中 取切片元素 与 切片再生成切片的区别

    func main() { //定义一个切片 a := []byte{1, 2, 3} fmt.Printf("a的类型%T\n", a) //a的类型[]uint8 fmt.Pr ...

  7. 详解最新版RabbitMQ 基于RPM 方式的安装

    如何选择安装版本 已经不支持的发布系列 版本 最后补丁版本 首次发布时间 停止更新时间 3.7 3.7.28 2017年11月28日 2020年09月30日 3.6 3.6.16 2015年12月22 ...

  8. 大米新闻微信小程序和Springboot新闻管理系统项目源码

    介绍 本项目分为大米news小程序端和springboot新闻管理系统后台项目.小程序主要用来新闻展示,后台管理系统用于提供相关新闻API. 项目源码 参考:https://www.bilibili. ...

  9. 【Android 逆向】【攻防世界】boomshakalaka-3

    1. apk 安装到手机,是一个cocos2dx 写的打飞机的游戏 题目描述跟得分有关(题目描述: play the game, get the highest score) 2. jadx 打开ap ...

  10. [BUUCTF][WEB][极客大挑战 2019]PHP 1

    打开靶机URL 看到字面提示 因为每次猫猫都在我键盘上乱跳,所以我有一个良好的备份网站的习惯不愧是我!!! 说明该网站有备份,说不定放在了Http服务器的某个目录下 那么这里我们可以用dirsearc ...