开发者

Measuring a program's running time

开发者 https://www.devze.com 2022-12-18 21:54 出处:网络
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). opr开发者_开发技巧ofile seems can do it, I will try to learn how to

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). opr开发者_开发技巧ofile 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?


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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消