测量程序的运行时间

发布于 2024-08-20 18:06:50 字数 147 浏览 6 评论 0原文

我需要一个工具来测量程序的运行时间,比如 gprof。但gprof的分辨率不够好(大约0.01秒)。 oprofile 似乎可以做到这一点,我会尝试学习如何获取有关时间信息的数据,但我不能。

那么,谁可以告诉我具体操作步骤,或者有人知道其他工具可以做同样的事情吗?

I need a tool to measure a program's running time, like gprof. But the resolution of gprof is not good enough (about 0.01 second). oprofile seems can do it, I will try to learn how to get the data about the time info, but I can't.

So, who can tell me the steps how to do it, or anyone knows other tool can do the same thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

メ斷腸人バ 2024-08-27 18:06:50

测量整个程序执行的运行时间在高分辨率下很少有用;当您分析事物时,您通常不想包含太多的开销。

通常最好只测量某些关键路径的执行时间,即使这样,多次重复执行该路径通常也是一个好主意,以提高计时准确性。

在Linux/POSIX系统下,经常使用 gettimeofday()对于这样的计时,它具有微秒精度:

#include <sys/time.h>
#include <time.h>
#include <stdio.h>

int main(void)
{
  struct timeval then, now;
  int i;

  gettimeofday(&then, NULL);
  for(i = 0; i < 100000; i++)
    my_interesting_function();
  gettimeofday(&now, NULL);

  printf("Did %d executions in %.3g seconds\n", i, now.tv_sec - then.tv_sec + 1e-6 * (now.tv_usec - then.tv_usec));

  return 0;
}

上面假设 my_interesting_function() 是您要测量其性能的函数。当然,根据函数的实际运行时间调整重复次数。

Measuring runtime over a whole program's execution is seldom useful with high resolution; there's too much overhead that you generally don't want to include when you're profiling things.

It's often better to measure the execution time of some critical path only, and even then it's often a good idea to repeat the execution of that path many times, to improve timing accuracy.

Under Linux/POSIX systems, gettimeofday() is often used for such timing, it has microsecond precision:

#include <sys/time.h>
#include <time.h>
#include <stdio.h>

int main(void)
{
  struct timeval then, now;
  int i;

  gettimeofday(&then, NULL);
  for(i = 0; i < 100000; i++)
    my_interesting_function();
  gettimeofday(&now, NULL);

  printf("Did %d executions in %.3g seconds\n", i, now.tv_sec - then.tv_sec + 1e-6 * (now.tv_usec - then.tv_usec));

  return 0;
}

The above assumes that my_interesting_function() is the function whose performance you want to measure. Of course tweak the number of repetitions depending on the actual runtime of the function.

拍不死你 2024-08-27 18:06:50

尽管这是一篇旧帖子,但我想我应该发布一些我认为对于测量进程运行时间非常有用的代码,因为它可能对其他人有用。

#include <sys/time.h>
#include <sys/resource.h>

typedef struct tag_time_measure
{
  struct timeval startTimeVal;
  struct timeval stopTimeVal;

  struct rusage startTimeUsage;
  struct rusage stopTimeUsage;
} time_measure;

time_measure * startTimeMeasuring()
{
  time_measure * tu = malloc(sizeof(time_measure));
  if(!tu)
    exit(1);

  getrusage(RUSAGE_SELF, &tu->startTimeUsage);
  gettimeofday(&tu->startTimeVal,0);

  return tu;
}

void stopTimeMeasuring(time_measure * tu)
{
  getrusage(RUSAGE_SELF, &tu->stopTimeUsage);
  gettimeofday(&tu->stopTimeVal,0);
}

void printMeasuredTime(time_measure * tu)
{
  struct timeval elapsedVal;
  struct timeval userVal;
  struct timeval systemVal;

  double elapsed_millis = 0.0f;
  double user_millis = 0.0f;
  double system_millis = 0.0f;

  timersub(&tu->stopTimeVal, &tu->startTimeVal, &elapsedVal);
  timersub(&tu->stopTimeUsage.ru_utime, &tu->startTimeUsage.ru_utime, &userVal);
  timersub(&tu->stopTimeUsage.ru_stime, &tu->startTimeUsage.ru_stime, &systemVal);

  elapsed_millis = elapsedVal.tv_sec * 1000 + (double) elapsedVal.tv_usec / 1000;
  user_millis = userVal.tv_sec * 1000 + (double) userVal.tv_usec / 1000;
  system_millis = systemVal.tv_sec * 1000 + (double) systemVal.tv_usec / 1000;

  printf("\n\n---Program execution times in milliseconds--- \n");
  printf("Total:\t\t %f\nUser:\t\t %f\nSystem:\t\t %f\n", elapsed_millis, user_millis, system_millis);
}

然后可以使用以下功能:

int main(void)
{
  time_measure * tu = startTimeMeasuring();

  doSomethingExpensiveHere();

  stopTimeMeasuring(tu);

  printMeasuredTime(tu);

  free(tu);

  return EXIT_SUCCESS;
}

可以轻松扩展代码以利用 rusage 提供的其他巧妙的东西(请参阅 http://www.gnu.org/s/libc/manual/html_node/Resource-Usage.html 了解更多详细信息)。希望有人会觉得有用:)

问候,
瓦西尔

even though it's an old post, I thought I'd post some code I find quite useful for measuring runtime of a process, since it might be useful to someone else.

#include <sys/time.h>
#include <sys/resource.h>

typedef struct tag_time_measure
{
  struct timeval startTimeVal;
  struct timeval stopTimeVal;

  struct rusage startTimeUsage;
  struct rusage stopTimeUsage;
} time_measure;

time_measure * startTimeMeasuring()
{
  time_measure * tu = malloc(sizeof(time_measure));
  if(!tu)
    exit(1);

  getrusage(RUSAGE_SELF, &tu->startTimeUsage);
  gettimeofday(&tu->startTimeVal,0);

  return tu;
}

void stopTimeMeasuring(time_measure * tu)
{
  getrusage(RUSAGE_SELF, &tu->stopTimeUsage);
  gettimeofday(&tu->stopTimeVal,0);
}

void printMeasuredTime(time_measure * tu)
{
  struct timeval elapsedVal;
  struct timeval userVal;
  struct timeval systemVal;

  double elapsed_millis = 0.0f;
  double user_millis = 0.0f;
  double system_millis = 0.0f;

  timersub(&tu->stopTimeVal, &tu->startTimeVal, &elapsedVal);
  timersub(&tu->stopTimeUsage.ru_utime, &tu->startTimeUsage.ru_utime, &userVal);
  timersub(&tu->stopTimeUsage.ru_stime, &tu->startTimeUsage.ru_stime, &systemVal);

  elapsed_millis = elapsedVal.tv_sec * 1000 + (double) elapsedVal.tv_usec / 1000;
  user_millis = userVal.tv_sec * 1000 + (double) userVal.tv_usec / 1000;
  system_millis = systemVal.tv_sec * 1000 + (double) systemVal.tv_usec / 1000;

  printf("\n\n---Program execution times in milliseconds--- \n");
  printf("Total:\t\t %f\nUser:\t\t %f\nSystem:\t\t %f\n", elapsed_millis, user_millis, system_millis);
}

Then one could use the functions like:

int main(void)
{
  time_measure * tu = startTimeMeasuring();

  doSomethingExpensiveHere();

  stopTimeMeasuring(tu);

  printMeasuredTime(tu);

  free(tu);

  return EXIT_SUCCESS;
}

Code can easily be extended to take advantage of the other neat stuff rusage offers (see http://www.gnu.org/s/libc/manual/html_node/Resource-Usage.html for more details). Hope someone will find that useful :)

Regards,
Vassil

牵你的手,一向走下去 2024-08-27 18:06:50

如果您使用的是 Linux/MacOSX/Unix,您始终可以使用 time 命令。

即使在超级计算机上它也相当准确。时间以秒为单位记录,精确到 3 位数字(即:1.034 秒)。唯一的问题是它将测量应用程序的整个运行时间,您不能仅测量子例程。

用法示例:

time ./myApplication

否则您将不得不编写一个计时器类。

If you are on Linux/MacOSX/Unix you can always use the time command.

It is fairly accurate even on a super computer. Time is recorded in seconds down to 3 digits of precision (ie: 1.034s). The only issue is that it will be measuring the whole run time of the application, you cannot measure sub routines only.

example usage:

time ./myApplication

Otherwise you will have to write a timer class.

岛歌少女 2024-08-27 18:06:50

如果您只想计时,请迭代 10^3 或 10^6 次,如 unwind 所说,然后秒表。 (我确实使用我的手表。)

如果您想找出导致速度变慢的原因(一个非常不同的问题),您可以比 gprof 做得更好。 试试这个。
如果您有兴趣,这里是对 gprof 的批评。

If you just want to time it, iterate it 10^3 or 10^6 times, as unwind said, and stopwatch it. (I literally use my watch.)

If you want to find out what is making it slow (a very different problem) you can do better than gprof. Try this.
If you're interested, here's a critique of gprof.

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