0

How can I keep track of how long my program executed a certain instruction in, for example, if one were to measure the diskusage, that would take time, and at the end my program should give an oupit along the lines of

real=50.0s user=30.s sys=20.0s

How do I distinguish and gather the system time, and the actual command time?

masoud
  • 55,379
  • 16
  • 141
  • 208
NoNameY0
  • 612
  • 2
  • 8
  • 18

3 Answers3

0

Back in the day I used to use pureCoverage to examine the execution time, but it was expensive. There might be free tools that do the same sort of thing, but for a quick and dirty solution...

include <time.h>
#include <stdlib.h>
#include <stdio.h>

int main(void) {
  time_t start_time, end_time;
  start_time = time(NULL);
  //Do your thing...
  end_time  = time(NULL);

  int diff = end_time - start_time;
  //Diff will be the number of milliseconds that elapsed.
}
Pete B.
  • 3,188
  • 6
  • 25
  • 38
0

Use getrusage in Linux:

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

...

struct rusage rusage;

getrusage(RUSAGE_SELF, &rusage);
/* use rusage.ru_utime and rusage.ru_stime for user and system time resp. */
nneonneo
  • 171,345
  • 36
  • 312
  • 383
0

There's another solution, not much different from the previous example.

#include<time.h>
int main ()
{
    clock_t start_time, stop_time;

    start_time = clock();

    // ... do your thing;

    stop_time = clock();

    printf("Elapsed time: %lf", (double)( stop_time - start_time ) / CLOCKS_PER_SEC);
}
Zoltán
  • 678
  • 4
  • 15