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