2004-03-17 23:22:08 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
|
|
|
|
unsigned long micro = 1;
|
|
|
|
|
|
|
|
void myusec_delay(int time)
|
|
|
|
{
|
|
|
|
volatile unsigned long i;
|
2004-03-20 17:46:10 +01:00
|
|
|
for (i = 0; i < time * micro; i++);
|
2004-03-17 23:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void myusec_calibrate_delay()
|
|
|
|
{
|
|
|
|
int count = 1000;
|
|
|
|
unsigned long timeusec;
|
|
|
|
struct timeval start, end;
|
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
printf("Setting up microsecond timing loop\n");
|
|
|
|
while (!ok) {
|
|
|
|
gettimeofday(&start, 0);
|
|
|
|
myusec_delay(count);
|
|
|
|
gettimeofday(&end, 0);
|
2004-03-20 17:46:10 +01:00
|
|
|
timeusec = 1000000 * (end.tv_sec - start.tv_sec) +
|
|
|
|
(end.tv_usec - start.tv_usec);
|
2004-03-17 23:22:08 +01:00
|
|
|
count *= 2;
|
2004-03-20 17:46:10 +01:00
|
|
|
if (timeusec < 1000000 / 4)
|
2004-03-17 23:22:08 +01:00
|
|
|
continue;
|
|
|
|
ok = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compute one microsecond. That will be count / time
|
|
|
|
micro = count / timeusec;
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
fprintf(stderr, "%ldM loops per second\n", (unsigned long) micro);
|
2004-03-17 23:22:08 +01:00
|
|
|
}
|