测量多线程程序的时间

发布于 2024-12-12 00:43:02 字数 303 浏览 0 评论 0原文

我用函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱武穆 2024-12-19 00:43:02

如果您想要挂钟时间,并且可以使用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 with CLOCK_MONOTONIC if you're measuring intervals of time, and CLOCK_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.

情话难免假 2024-12-19 00:43:02

我认为clock()统计了所有线程中CPU的总使用率,我也遇到了这个问题......

挂钟计时方法的选择是个人喜好。我使用内联包装函数来获取时间戳(采用 2 个时间戳的差值来计算处理时间)。为了方便起见,我使用了浮点(单位以秒为单位,不必担心整数溢出)。对于多线程,有如此多的异步事件,在我看来,时间低于 1 微秒是没有意义的。到目前为止,这对我来说非常有效:)

无论您选择什么,包装器都是试验使用的最简单方法

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

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);

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文