简体   繁体   English

测量多线程程序的时间

[英]measuring time of multi-threading program

I measured time with function clock() but it gave bad results. 我用函数clock()测量了时间,但结果不好。 I mean it gives the same results for program with one thread and for the same program running with OpenMP with many threads. 我的意思是它为具有一个线程的程序和使用具有许多线程的OpenMP运行的相同程序提供相同的结果。 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() ? clock_gettime()或mb gettimeofday()? or mb something else? 或者别的什么?

if clock_gettime(),then with which clock? 如果clock_gettime(),那么用哪个时钟? CLOCK_REALTIME or CLOCK_MONOTONIC? CLOCK_REALTIME还是CLOCK_MONOTONIC?

using mac os x (snow leopard) 使用mac os x(雪豹)

If you want wall-clock time, and clock_gettime() is available, it's a good choice. 如果你想要挂钟时间,并且clock_gettime()可用,那么这是一个不错的选择。 Use it with CLOCK_MONOTONIC if you're measuring intervals of time, and CLOCK_REALTIME to get the actual time of day. 如果您正在测量时间间隔,请使用CLOCK_MONOTONIC ,使用CLOCK_REALTIME获取实际时间。

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_REALTIME为您提供实际的一天中的时间,但会受到系统时间调整的影响 - 因此,如果在程序运行时调整系统时间,则会使用它来查看间隔测量值。

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_MONOTONIC没有给你正确的时间,但它确实以相同的速率计数并且不受系统时间变化的影响 - 所以它非常适合测量间隔,但是当需要正确的时间来显示时,它是无用的对于时间戳。

I think clock() counts the total CPU usage among all threads, I had this problem too... 我认为clock()计算所有线程中的总CPU使用率,我也有这个问题...

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). 我使用内联包装函数来获取时间戳(使用2个时间戳的差异来计算处理时间)。 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. 对于多线程,有很多异步事件,在我看来,时间低于1微秒是没有意义的。 This has worked very well for me so far :) 到目前为止,这对我来说非常有效:)

Whatever you choose, a wrapper is the easiest way to experiment 无论您选择什么,包装都是最简单的实验方式

inline double my_clock(void) {
  struct timeval t;
  gettimeofday(&t, NULL);
  return (1.0e-6*t.tv_usec + t.tv_sec);
}

usage: 用法:

double start_time, end_time;
start_time = my_clock();
//some multi-threaded processing
end_time = my_clock();
printf("time is %lf\n", end_time-start_time);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM