Program runtime measurement

I need a tool to measure program runtime, for example gprof. But gprof resolution is not good enough (about 0.01 second). oprofile seems to be able to do this, I will try to find out how to get data about the time information, but I can not.

So, who can tell me how to do this, or does anyone know that another tool can do the same?

+3
source share
4 answers

Measuring runtime over the execution of an entire program is rarely useful at high resolution; there’s too much overhead that you usually don’t want to include when you profile things.

, , .

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() - , . , .

+6

, , , , - .

#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). , - :)

,

+2

Linux/MacOSX/Unix, time.

. ( : 1,034 ). , , .

:

time ./myApplication

.

0

, 10 ^ 3 10 ^ 6 , , . ( .)

If you want to know what is slowing down (a completely different problem), you can do better than gprof. Try it. If you're interested, here's a gprof criticism.

0
source

Source: https://habr.com/ru/post/1731346/


All Articles