File: simple.c

package info (click to toggle)
eztrace 2.0%2Brepack-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,132 kB
  • sloc: ansic: 23,501; perl: 910; sh: 857; cpp: 771; makefile: 696; fortran: 327; f90: 320; python: 57
file content (45 lines) | stat: -rw-r--r-- 920 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* -*- c-file-style: "GNU" -*- */
/*
 * Copyright (C) CNRS, INRIA, Université Bordeaux 1, Télécom SudParis
 * See COPYING in top-level directory.
 */

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

#define NB_ITER 1000000000
#define TIME_DIFF(t1, t2) ((t2.tv_sec-t1.tv_sec)*1e6+(t2.tv_usec-t1.tv_usec))

int foo_local() {
  return 42;
}

int main() {
  int i, j;
  unsigned res = 0;
  struct timeval t1, t2;

  for (j = 100; j < NB_ITER; j *= 10) {
    fprintf(stderr, "START %d iter!\n", j);
    if (gettimeofday(&t1, NULL)) {
      perror("gettimeofday");
    }

    for (i = 0; i < j; i++) {
      res += foo();
      res += foo_local();
    }
    /* fin de la mesure */
    if (gettimeofday(&t2, NULL)) {
      perror("gettimeofday");
    }
    fprintf(stderr, "STOP !\n");

    /* affichage du resultat */
    fprintf(stderr, "res = %d\n", res);

    printf("%d\t%lf\n", j, TIME_DIFF(t1, t2) / j);
  }

  return 0;
}