File: prntf-time.c

package info (click to toggle)
cmix 2.0.12-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,924 kB
  • ctags: 6,063
  • sloc: cpp: 27,155; ansic: 11,923; sh: 3,000; exp: 2,270; yacc: 1,724; makefile: 1,251; lex: 488; perl: 278
file content (60 lines) | stat: -rw-r--r-- 2,112 bytes parent folder | download | duplicates (4)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**** EXAMPLE SPECIFIC SECTION ****************************/

#define ARGS 2
extern int goal(int x, int n);
extern int goal_res(int x);
int x; int n;
#define READARGS     x = atoi(argv[1]); n = atoi(argv[2])
#define CALLSOURCE   goal  (x, n)
#define CALLRESID    goal_res(x)
#define FORMATRESULT "%d"

/**** END OF EXAMPLE SPECIFIC SECTION *********************/

#include <sys/times.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
  if (argc < ARGS + 2) {
    fprintf(stderr, "Usage: %s repeat arg1 ... arg%d\n", argv[0], ARGS);
    return -1;
  } else {
    struct tms tsourcestart, tsourcestop, tresstart, tresstop;
    double tsourceuser, tsourcesys, tresuser, tressys;
    long clk_tck;
    int i, repeat;
    repeat = atoi(argv[1]); argv++;
    READARGS;
    fprintf(stderr, "Timing source program...\n");
    times(&tsourcestart);
    for (i = 0; i < repeat; i++) CALLSOURCE;
    times(&tsourcestop);
    printf("Source   result: " FORMATRESULT "\n", CALLSOURCE);
    fprintf(stderr, "Timing residual program...\n");
    times(&tresstart);
    for (i = 0; i < repeat; i++) CALLRESID;
    times(&tresstop);
    printf("Residual result: " FORMATRESULT "\n", CALLRESID);
    fprintf(stderr, "Done.\n");
    clk_tck = sysconf(_SC_CLK_TCK);
    tsourceuser = (double)(tsourcestop.tms_utime - tsourcestart.tms_utime) /
      (double)clk_tck;
    tsourcesys = (double)(tsourcestop.tms_stime - tsourcestart.tms_stime) /
      (double)clk_tck;
    tresuser = (double)(tresstop.tms_utime - tresstart.tms_utime) /
      (double)clk_tck;
    tressys = (double)(tresstop.tms_stime - tresstart.tms_stime) /
      (double)clk_tck;
    printf("\n                  User time       (System time   )\n");
    printf("Source   program  %6.5g seconds  (%6.5g seconds)\n",
	   tsourceuser, tsourcesys);
    printf("Residual program  %6.5g seconds  (%6.5g seconds)\n",
	   tresuser, tressys);
    printf("Speedup factor    %6.1f          (%6.1f        )\n",
	   tresuser == 0.0 ? 1.0 : tsourceuser / tresuser,
	   tressys == 0.0 ? 1.0 : tsourcesys / tressys);
  }
  return 0;
}