在多线程C ++应用程序中测量时间 [英] Measuring time in a multithreaded C++ application

查看:101
本文介绍了在多线程C ++应用程序中测量时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++和OpenMP编写应用程序,我想可靠(并正确)测量执行部分的时间。我已经查看过几个选项(Windows,TDM-GCC x64 4.8.1):

I'm writing an application using C++ and OpenMP and I want to reliably (and correctly) measure time of execution of parts of it. I have reviewed a few options (Windows, TDM-GCC x64 4.8.1):


  1. omp_get_wproc clock 似乎完成这项工作,但文档(与实际行为直接相抵触)说,他们测量给定进程消耗的总时间资源(即,例如一秒,两个工作线程计数为两秒)。 正确的行为不是我想要的,

  2. 时间 / difftime 没有足够的分辨率,

  3. GetProcessTime 平台特定的,

  4. QueryPerformanceCounter (WinAPI)似乎是一种方式,但平台特定,

  5. C ++ 11 high_resolution_clock 工作正常,但它是新标准的一部分。

  1. omp_get_wproc and clock seem to do the job but documentation (in direct contradiction to actual behavior) says that they measure total time resources consumed by given process (that is, e.g. one second with two working threads counts as two seconds). The "correct" behavior is not what I want,
  2. time/difftime don't have enough resolution,
  3. GetProcessTime (WinAPI) does what clock should do and is platform-specific,
  4. QueryPerformanceCounter (WinAPI) seems to be the way to go but is platform-specific,
  5. C++11 high_resolution_clock works ok but it's a part of a new standard.

我的问题主要是:做科学计算的人如何做到这一点,为什么这样做?而且, clock 是我的标准库实现中的错误还是过于流行的误解?

My question, primarily, is: how do people doing scientific computing do this, and why do it that way? And, is the behavior of clock a bug in my implementation of standard library or a too popular misconception?


小解释:我有点犹豫使用C ++ 11,因为我可能在一个有些老的软件的集群上运行我的代码。

Small explanation: I'm a bit hesitant to use C++11 because I'll probably run my code on a cluster with somewhat old software.

推荐答案

您没有提及 boost :: chrono 。与C ++ 11相同 chrono ,但适用于C ++ 03编译器。

You didn't mention boost::chrono. Same as C++11 chrono, but works with C++03 compiler.

你对C ++ 11的犹豫。我们差不多在2015年,而C ++ 11不是那么新。它甚至不是最近的标准。因此, #include< chrono> 是一种方式。

Also, I cannot understand your hesitation about C++11. We are almost in 2015, and C++11 is not that new. It is even not the most recent standard. So, #include <chrono> is a way to go.

c> chrono 在Visual Studio 2013标准库实现中有些破坏。我个人使用 std :: chrono 无处不在,并通过有条件的<$ c $交换到 boost :: chrono c>定义和 typedef 。希望他们会在Visual Studio中修复它。

Note however, that chrono is somewhat broken in Visual Studio 2013 Standard library implementation. I, personally, use std::chrono everywhere and swap it to boost::chrono via conditional defines and typedefs. Hope they'll fix it in Visual Studio Next.

这篇关于在多线程C ++应用程序中测量时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆