http://stackoverflow.com/questions/2808398/easily-measure-elapsed-time

https://github.com/picanumber/bureaucrat/blob/master/time_lapse.h



#include <ctime>

void f() {
using namespace std;
clock_t begin = clock(); code_to_time(); clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
}

The time() function is only accurate to within a second, but there are CLOCKS_PER_SEC "clocks" within a second. This is an easy, portable measurement, even though it's over-simplified.

if (globals::gAMInstance->enableTrans()) {
clock_t begin = clock();
std::string ostr;
raw_string_ostream ostrstream(ostr);
ostrstream << *module;
std::ofstream ofs(globals::getOutputDir(ASSEMBLY_BEFORE_TRANS_LL_FILE));
ofs << ostrstream.str();
ofs.close();

outs() << "transforming ......\n";
legacy::PassManager PM;
// PM.add(new ProgTrans());
PM.run(*module);
outs() << "program transforming finished\n";
outs().flush();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
std::cerr << "Program transformation spends " << std::fixed
<< std::setprecision(4) << elapsed_secs << "s.\n";

// RWset RW(*globals::gCMInstance->getKModule()->module,
// shape::RWType::TRANS);
// RW.dump();

// RWset rwset(*(this->module), shape::RWType::STATE_CACHE);
// const Instruction &ins = module->begin()->back().front();
// errs() << "the live variables of " << ins << " :\n";
// auto s = rwset.readset(ins);
// s.dump();
// rwset.dump();
// exit(0);
}

You can abstract the time measuring mechanism and have each callable's run time measured with minimal extra code, just by being called through a timer structure. Plus, at compile time you can parametrize the timing type (milliseconds, nanoseconds etc).

Thanks to the review by Loki Astari and the suggestion to use variadic templates. This is why the forwarded function call.

#include <iostream>
#include <chrono> template<typename TimeT = std::chrono::milliseconds>
struct measure
{
template<typename F, typename ...Args>
static typename TimeT::rep execution(F&& func, Args&&... args)
{
auto start = std::chrono::steady_clock::now();
std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast< TimeT>
(std::chrono::steady_clock::now() - start);
return duration.count();
}
}; int main() {
std::cout << measure<>::execution(functor(dummy)) << std::endl;
}

Demo

According to the comment by Howard Hinnant it's best not to escape out of the chrono system until we have to. So the above class could give the user the choice to call count manually by providing an extra static method (shown in C++14)

template<typename F, typename ...Args>
static auto duration(F&& func, Args&&... args)
{
auto start = std::chrono::steady_clock::now();
std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
return std::chrono::duration_cast<TimeT>(std::chrono::steady_clock::now()-start);
} // call .count() manually later when needed (eg IO)
auto avg = (measure<>::duration(func) + measure<>::duration(func)) / 2.0;

and be most useful for clients that

"want to post-process a bunch of durations prior to I/O (e.g. average)"


The complete code can be found here. My attempt to build a benchmarking tool based on chrono is recorded here.


If C++17's std::invoke is available, the invocation of the callable in execution could be done like this :

invoke(forward<decltype(func)>(func), forward<Args>(args)...);

to provide for callables that are pointers to member functions.

C/C++ 记录时间的更多相关文章

  1. [转]IIS 日志记录时间和实际时间 不一样

    今天偶然发现 2003 系统IIS 日志记录时间和实际时间总是差了8个小时,也就是慢了8个小时.苦苦找了半天才发现如下办法能解决 ,特发来分享下 解决1:如果 IIS日志记录默认使用的是W3C扩展日志 ...

  2. Sql查询今天、本周和本月的记录(时间字段为时间戳)

    工作中遇到的问题,小结一下 查询今日添加的记录: select * from [表名] where datediff(day,CONVERT(VARCHAR(20),DATEADD(SECOND,[时 ...

  3. window.setTimeout和window.setInterval的区别,及用其中一个方法记录时间。

    window.setTimeout(语句,时间)是在多久之后执行语句,语句只执行一次. window.setInterval(语句,时间)是每隔多久执行一次语句,语句循环执行. <!DOCTYP ...

  4. Linux系统如何记录时间

    1.内核在开机启动的时候会读取RTC硬件获取一个时间作为初始基准时间,这个基准时间对应一个jiiffies值(这个基准时间换算成jiffies值的方法是:用这个时间减去1970-01-01  00:0 ...

  5. 记录——时间轮定时器(lua 实现)

    很长一段时间里,我错误的认识了定时器.无意中,我发现了“时间轮”这个名词,让我对定时器有了新的看法. 我错误的认为,定时器只需要一个 tick 队列,按指定的时间周期遍历队列,检查 tick 倒计时满 ...

  6. JavaScript创建日志文件并记录时间的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 try { var WSShell = WScript.CreateObject("WScript.Shel ...

  7. C# txt格式记录时间,时间对比,决定是否更新代码记录Demo

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. FindControl什么时候才会使用ObjectFromHWnd函数呢?——VCL很难调试,加一个日志函数,记录时间

    IsDelphiHandleFindVCLWindowfunction IsVCLControl(Handle: HWND): Boolean;function FindControl(Handle: ...

  9. Oracle 查询当前系统时间十分钟之前的记录,时间比较SQL

    select * from t_register r ));

随机推荐

  1. 使用appium进行ios测试,启动inspector时遇到的问题(一)

    最近在公司,让做ios的自动化测试,因为以前做过android的自动化测试,用的也是appium,觉得没什么,结果一开始在搭建环境就遇到了很多的问题,现在将我遇到的问题,以及解决方法,给大家分享出来. ...

  2. 安卓通用shell大全

    一.[什么是shell] Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口.它是命令语言.命令解释程序及程序设计语言的统称.shell是用户和Linux内核之间的接口程序,如 ...

  3. android上下文菜单

    XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmln ...

  4. 搭建fedora开发环境 common lisp, c++, go

    第三方软件库: http://download1.rpmfusion.org/free/fedora/releases/25/Everything/x86_64/os/repoview/index.h ...

  5. css内容样式属性

    设置元素的最大高度.最小高度.最大宽度.最小宽度,用max-height.min-height.max-width.min-width. visibility:设置元素是否可见.visible和hid ...

  6. TFS 2010 迁移/重装/还原 步骤

    1.签入所有代码 2.停止TFS服务:运行命令行,并将路径切换到TFS安装路径:C:\Program Files\Microsoft Team Foundation Server 2010\Tools ...

  7. Appium常见问题(持续更新)

    1.Original error: Could not parse activity from dumpsys 命令行:adb shell  dumpsys cpuinfo  报:service du ...

  8. 41个Web开发者JavaScript实用小技巧

    1. 将彻底屏蔽鼠标右键 oncontextmenu="window.event.returnValue=false" < table border oncontextmen ...

  9. Maximo-删除应用程序

    执行如下SQL: delete from maxapps where app='<APPLICATION NAME>';delete from maxpresentation where  ...

  10. Android四大组件及activity的四大启动模式

    Android四大组件 1. 广播接收者的两种类型: (1)系统广播接收者,就是继承BroadcastReceiver这个类,然后还要在清单文件中注册,注册之后给他一个action.当系统发生了这个a ...