测量多线程程序的时间
我用函数 Clock() 测量了时间,但结果很差。我的意思是,对于具有一个线程的程序和使用 OpenMP 运行的具有多个线程的同一程序,它会给出相同的结果。但事实上,我通过手表注意到,线程较多的程序计数速度更快。 所以我需要一些挂钟定时器...
我的问题是:对于这个问题有什么更好的功能? Clock_gettime() 或 mb gettimeofday() ?或者mb其他什么?
如果是clock_gettime(),那么使用哪个时钟? CLOCK_REALTIME 还是 CLOCK_MONOTONIC?
使用 mac os x(雪豹)
I measured time with function clock() but it gave bad results. I mean it gives the same results for program with one thread and for the same program running with OpenMP with many threads. But in fact, I notice with my watch that with many threads program counts faster.
So I need some wall-clock timer...
My question is: What is better function for this issue?
clock_gettime() or mb gettimeofday() ? or mb something else?
if clock_gettime(),then with which clock? CLOCK_REALTIME or CLOCK_MONOTONIC?
using mac os x (snow leopard)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要挂钟时间,并且可以使用
clock_gettime()
,那么它是一个不错的选择。如果您要测量时间间隔,请将其与CLOCK_MONOTONIC
一起使用,并与CLOCK_REALTIME
一起使用以获取一天中的实际时间。CLOCK_REALTIME
为您提供一天中的实际时间,但会受到系统时间调整的影响 - 因此,如果在程序运行时调整系统时间,则会扰乱使用它的间隔测量。CLOCK_MONOTONIC
不会为您提供一天中的正确时间,但它确实以相同的速率计数并且不受系统时间变化的影响 - 因此它非常适合测量间隔,但在正确时间时毫无用处显示或时间戳需要当天的日期。If you want wall-clock time, and
clock_gettime()
is available, it's a good choice. Use it withCLOCK_MONOTONIC
if you're measuring intervals of time, andCLOCK_REALTIME
to get the actual time of day.CLOCK_REALTIME
gives you the actual time of day, but is affected by adjustments to the system time -- so if the system time is adjusted while your program runs that will mess up measurements of intervals using it.CLOCK_MONOTONIC
doesn't give you the correct time of day, but it does count at the same rate and is immune to changes to the system time -- so it's ideal for measuring intervals, but useless when correct time of day is needed for display or for timestamps.我认为clock()统计了所有线程中CPU的总使用率,我也遇到了这个问题......
挂钟计时方法的选择是个人喜好。我使用内联包装函数来获取时间戳(采用 2 个时间戳的差值来计算处理时间)。为了方便起见,我使用了浮点(单位以秒为单位,不必担心整数溢出)。对于多线程,有如此多的异步事件,在我看来,时间低于 1 微秒是没有意义的。到目前为止,这对我来说非常有效:)
无论您选择什么,包装器都是试验使用的最简单方法
:
I think clock() counts the total CPU usage among all threads, I had this problem too...
The choice of wall-clock timing method is personal preference. I use an inline wrapper function to take time-stamps (take the difference of 2 time-stamps to time your processing). I've used floating point for convenience (units are in seconds, don't have to worry about integer overflow). With multi-threading, there are so many asynchronous events that in my opinion it doesn't make sense to time below 1 microsecond. This has worked very well for me so far :)
Whatever you choose, a wrapper is the easiest way to experiment
usage: