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. 解决window2012 IIS8 配置的网站无法下载exe文件的问题

    window2012 IIS8 配置网站下载exe文件.解决window2012 IIS8 配置的网站无法下载exe文件的问题 配置好网站后,无法下载网站上的exe文件,zip文件确可以下载的.右键点 ...

  2. qt环境下Mapx组建的编程---------regoin

    #include "widget.h" #include "ui_widget.h" #include <QPushButton> #include ...

  3. RunTime的简单使用

    Runtime也就是运行时,是苹果官方提供的一套C语音库,那有什么作用呢?简单的来说,Runtime可以做很多的底层操作,比如说访问隐藏的一些成员变量,成员方法,当然包括了私有的成员变量,成员方法. ...

  4. ubuntu软件推荐

    本文推荐的ubuntu工具均为笔者亲用.原则:在精不在多. 0.万能类 笔者崇尚[极简主义],常用的工具如果有网页版的就尽量不用单独的client.如网页版微信. 1.系统类 截图:Deepin-sc ...

  5. fcitx jdk maven profile配置

    #小企鹅输入法配置 export GTK_IM_MODULE=fcitx export QT_IM_MODULE=fcitx export XMODIFIERS="@im=fcitx&quo ...

  6. c#接口容易被忽视的问题

    今天在看"并发集合"的时候,接口IProducerConsumerCollection<T> 有一个方法是TryAdd(),表示"试图"去添加,然后 ...

  7. Python爬虫抓取糗百的图片,并存储在本地文件夹

    思路: 1.观察网页,找到img标签 2.通过requests和BS库来提取网页中的img标签 3.抓取img标签后,再把里面的src给提取出来,接下来就可以下载图片了 4.通过urllib的urll ...

  8. WPF打印、预览、导出PDF

    本人很懒,已找到可使用样例 例:   http://www.cnblogs.com/guogangj/archive/2013/02/27/2934733.html

  9. ArcEngine中打开各种数据源(WorkSpace)的连接

    (SDE.personal/File.ShapeFile.CAD数据.影像图.影像数据集) ArcEngine 可以接受多种数据源.在开发过程中我们使用了如下几种数据源 1.企业数据库(SDE) 企业 ...

  10. LAMP环境

    LAMP =  Linux + Apache + MySQL + PHP    [1]     [2]      [3]     [4] [1]Linux是一套免费使用和自由传播的类Unix操作系统, ...