Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame^] | 1 | #include <sys/time.h> |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | // count to a billion. Time it. If it's < 1 sec, count to 10B, etc. |
| 5 | unsigned long micro = 1; |
| 6 | |
| 7 | void myusec_delay(int time) |
| 8 | { |
| 9 | volatile unsigned long i; |
| 10 | for(i = 0; i < time * micro; i++) |
| 11 | ; |
| 12 | } |
| 13 | |
| 14 | void myusec_calibrate_delay() |
| 15 | { |
| 16 | int count = 1000; |
| 17 | unsigned long timeusec; |
| 18 | struct timeval start, end; |
| 19 | int ok = 0; |
| 20 | void myusec_delay(int time); |
| 21 | |
| 22 | printf("Setting up microsecond timing loop\n"); |
| 23 | while (!ok) { |
| 24 | //fprintf(stderr, "Try %d\n", count); |
| 25 | gettimeofday(&start, 0); |
| 26 | myusec_delay(count); |
| 27 | gettimeofday(&end, 0); |
| 28 | timeusec = 1000000 * (end.tv_sec - start.tv_sec ) + |
| 29 | (end.tv_usec - start.tv_usec); |
| 30 | //fprintf(stderr, "timeusec is %d\n", timeusec); |
| 31 | count *= 2; |
| 32 | if (timeusec < 1000000/4) |
| 33 | continue; |
| 34 | ok = 1; |
| 35 | } |
| 36 | |
| 37 | // compute one microsecond. That will be count / time |
| 38 | micro = count / timeusec; |
| 39 | |
| 40 | fprintf(stderr, "%ldM loops per second\n", (unsigned long)micro); |
| 41 | |
| 42 | |
| 43 | } |