show version
[tridge/junkcode.git] / speed.c
1 #define _GNU_SOURCE
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <math.h>
8 #include <sys/time.h>
9
10
11 #define MAX(a,b) ((a)>(b)?(a):(b))
12 #define MIN(a,b) ((a)<(b)?(a):(b))
13
14 /* for solaris */
15 #ifdef SOLARIS
16 #include <limits.h>
17 #include <sys/times.h>
18 struct tms tp1,tp2;
19
20 static void start_timer()
21 {
22   times(&tp1);
23 }
24
25 static double end_timer()
26 {
27   times(&tp2);
28   return((tp2.tms_utime - tp1.tms_utime)/(1.0*CLK_TCK));
29 }
30
31 #elif (defined(LINUX))
32
33 #include <sys/resource.h>
34 struct rusage tp1,tp2;
35
36 static void start_timer()
37 {
38   getrusage(RUSAGE_SELF,&tp1);
39 }
40
41
42 static double end_timer()
43 {
44   getrusage(RUSAGE_SELF,&tp2);
45   return((tp2.ru_utime.tv_sec - tp1.ru_utime.tv_sec) + 
46          (tp2.ru_utime.tv_usec - tp1.ru_utime.tv_usec)*1.0e-6);
47 }
48
49 #else
50
51 #include <sys/time.h>
52
53 struct timeval tp1,tp2;
54
55 static void start_timer()
56 {
57   gettimeofday(&tp1,NULL);
58 }
59
60 static double end_timer()
61 {
62   gettimeofday(&tp2,NULL);
63   return((tp2.tv_sec - tp1.tv_sec) + 
64          (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
65 }
66
67 #endif
68
69 static FILE *f;
70
71 static void dumpval(double val)
72 {
73   fwrite((void *)&val,sizeof(val),1,f);
74 }
75
76
77
78 static void memcpy_test(int size)
79 {
80   int i;
81   int loops = 5.0e8 / size;
82   char *p1 = (char *)malloc(size+100);
83   char *p2 = (char *)malloc(size+100);
84   double t;
85
86   memset(p2,42,size);
87   start_timer();
88
89   for (i=0;i<loops;i++)
90     {
91       memcpy(p1+16,p2,size);
92       memcpy(p2+16,p1,size);
93     }
94
95   t = end_timer();
96   dumpval(*(double *)p1);
97   dumpval(*(double *)p2);
98   free(p1); free(p2);
99
100   printf("%g Mb/S\n",i*2.0*size/(1.0e6*t));
101 }
102
103
104 int main(void)
105 {
106   int loops = 100000;
107   int l;
108   double d=0;
109   double t;
110   int i;
111
112 #define ADD_SIZE 1*1024
113   
114   f = fopen("/dev/null","w");
115   
116   printf("Floating point - sin() - ");
117   l = 1024;
118   start_timer();
119   for (i=0;i<loops;i++)
120     d += sin((double)i);
121   t = end_timer();
122   printf("%g MOPS\n",loops/(1.0e6*t));
123   dumpval(d);
124
125   printf("Floating point - log() - ");
126   l = 1024;
127   start_timer();
128   for (i=0;i<loops;i++)
129     d += log((double)(i+1));
130   t = end_timer();
131   printf("%g MOPS\n",loops/(1.0e6*t));
132   dumpval(d);
133
134   printf("Memcpy - 1kB - ");
135   memcpy_test(1024);
136
137   printf("Memcpy - 100kB - ");
138   memcpy_test(1024*100);
139
140   printf("Memcpy - 1MB - ");
141   memcpy_test(1024*1024);
142
143   printf("Memcpy - 10MB - ");
144   memcpy_test(1024*1024*10);
145
146   loops *= 10;
147   printf("Adding integers - ");
148   l = ADD_SIZE;
149   {
150     int *p1 = (int *)malloc(l*sizeof(int));
151     int sum;
152     for (i=0;i<l;i++)
153       p1[i] = i;
154     start_timer();
155
156     for (i=0;i<loops/100;i++)
157       {
158         int j;
159         sum = i;
160         for (j=0;j<l;j++)
161           sum += p1[j];
162       }
163     t = end_timer();
164     dumpval(sum);
165     free(p1);
166   }
167   printf("%g MOPS\n",0.01*(loops*l)/(1.0e6*t));
168
169   printf("Adding floats (size %d) - ",sizeof(float));
170   l = ADD_SIZE;
171   {
172     float *p1 = (float *)malloc(l*sizeof(float));
173     float sum;
174     for (i=0;i<l;i++)
175       p1[i] = i;
176     start_timer();
177     for (i=0;i<loops/100;i++)
178       {
179         int j;
180         sum = i;
181         for (j=0;j<l;j++)
182           sum += p1[j];
183       }
184     t = end_timer();
185     dumpval(sum);
186     free(p1);
187   }
188   printf("%g MOPS\n",0.01*(loops*l)/(1.0e6*t));
189
190   printf("Adding doubles (size %d) - ",sizeof(double));
191   l = ADD_SIZE;
192   {
193     double *p1 = (double *)malloc(l*sizeof(double));
194     double sum;
195     for (i=0;i<l;i++)
196       p1[i] = i;
197     start_timer();
198     for (i=0;i<loops/100;i++)
199       {
200         int j;
201         sum = i;
202         for (j=0;j<l;j++)
203           sum += p1[j];
204       }
205     t = end_timer();
206     dumpval(sum);
207     free(p1);
208   }
209   printf("%g MOPS\n",0.01*(loops*l)/(1.0e6*t));
210
211   return 0;
212 }