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; |
Ollie Lho | cec2879 | 2004-03-17 23:03:37 +0000 | [diff] [blame] | 10 | for (i = 0; i < time * micro; i++) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 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; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 20 | |
| 21 | printf("Setting up microsecond timing loop\n"); |
| 22 | while (!ok) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 23 | gettimeofday(&start, 0); |
| 24 | myusec_delay(count); |
| 25 | gettimeofday(&end, 0); |
| 26 | timeusec = 1000000 * (end.tv_sec - start.tv_sec ) + |
| 27 | (end.tv_usec - start.tv_usec); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 28 | count *= 2; |
| 29 | if (timeusec < 1000000/4) |
| 30 | continue; |
| 31 | ok = 1; |
| 32 | } |
| 33 | |
| 34 | // compute one microsecond. That will be count / time |
| 35 | micro = count / timeusec; |
| 36 | |
| 37 | fprintf(stderr, "%ldM loops per second\n", (unsigned long)micro); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 38 | } |