added -D
[tridge/junkcode.git] / systime.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <string.h>
5 #include <sys/stat.h>
6 #include <sys/resource.h>
7 #include <unistd.h>
8
9 static struct timeval tp1,tp2;
10
11 static double tvdiff(struct timeval *tv1, struct timeval *tv2)
12 {
13         return (tv2->tv_sec + (tv2->tv_usec*1.0e-6)) - 
14                 (tv1->tv_sec + (tv1->tv_usec*1.0e-6));
15 }
16
17 static void start_timer()
18 {
19         gettimeofday(&tp1,NULL);
20 }
21
22 static double end_timer()
23 {
24         gettimeofday(&tp2,NULL);
25         fflush(stdout);
26         return tvdiff(&tp1, &tp2);
27 }
28
29
30 static void load_file(void)
31 {
32         start_timer();
33         while (end_timer() < 5) {
34                 int i;
35                 for (i=0;i<1000;i++) {
36                         struct stat st;
37                         stat(".", &st);
38                 }
39         }
40 }
41
42 static void load_cpu(void)
43 {
44         start_timer();
45         while (end_timer() < 5) {
46                 int i;
47                 for (i=0;i<1000;i++) {
48                         char x1[10000];
49                         char x2[10000];
50                         memcpy(x1, x2, sizeof(x2));
51                 }
52         }
53 }
54
55
56 int main(void)
57 {
58         struct rusage r1, r2;
59
60         getrusage(RUSAGE_SELF, &r1);
61         load_file();
62         getrusage(RUSAGE_SELF, &r2);
63
64         printf("file system %g\n", tvdiff(&r1.ru_stime, &r2.ru_stime));
65         printf("file user   %g\n", tvdiff(&r1.ru_utime, &r2.ru_utime));
66
67         getrusage(RUSAGE_SELF, &r1);
68         load_cpu();
69         getrusage(RUSAGE_SELF, &r2);
70         printf("cpu  system %g\n", tvdiff(&r1.ru_stime, &r2.ru_stime));
71         printf("cpu  user   %g\n", tvdiff(&r1.ru_utime, &r2.ru_utime));
72
73         printf("\n");
74         return 0;
75 }