c gettimeofday example

Solutions on MaxInterview for c gettimeofday example by the best coders in the world

showing results for - "c gettimeofday example"
Jesús
07 Oct 2018
1#include <time.h>
2#include <sys/time.h>
3#include <stdlib.h>
4#include <stdio.h>
5
6int main(int argc, char **argv)
7{
8  if (argc < 2)
9    {
10      printf("USAGE: %s loop-iterations\n", argv[0]);
11      return 1;
12    }
13
14  int iterations = atoi(argv[1]);
15
16  struct timeval start, end;
17
18  gettimeofday(&start, NULL);
19
20  for (int i = 0; i < iterations; i++)
21    {
22    }
23
24  gettimeofday(&end, NULL);
25
26  printf("%ld\n", ((end.tv_sec * 1000000 + end.tv_usec)
27		  - (start.tv_sec * 1000000 + start.tv_usec)));
28
29  return 0;
30}
31