blob: 30b8dde26180e8e3f2cbab0b139f024b98ad8041 [file] [log] [blame]
Ollie Lhocbbf1252004-03-17 22:22:08 +00001#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.
5unsigned long micro = 1;
6
7void myusec_delay(int time)
8{
9 volatile unsigned long i;
Ollie Lho761bf1b2004-03-20 16:46:10 +000010 for (i = 0; i < time * micro; i++);
Ollie Lhocbbf1252004-03-17 22:22:08 +000011}
12
13void myusec_calibrate_delay()
14{
15 int count = 1000;
16 unsigned long timeusec;
17 struct timeval start, end;
18 int ok = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +000019
20 printf("Setting up microsecond timing loop\n");
21 while (!ok) {
Ollie Lhocbbf1252004-03-17 22:22:08 +000022 gettimeofday(&start, 0);
23 myusec_delay(count);
24 gettimeofday(&end, 0);
Ollie Lho761bf1b2004-03-20 16:46:10 +000025 timeusec = 1000000 * (end.tv_sec - start.tv_sec) +
26 (end.tv_usec - start.tv_usec);
Ollie Lhocbbf1252004-03-17 22:22:08 +000027 count *= 2;
Ollie Lho761bf1b2004-03-20 16:46:10 +000028 if (timeusec < 1000000 / 4)
Ollie Lhocbbf1252004-03-17 22:22:08 +000029 continue;
30 ok = 1;
31 }
32
33 // compute one microsecond. That will be count / time
34 micro = count / timeusec;
35
Ollie Lho761bf1b2004-03-20 16:46:10 +000036 fprintf(stderr, "%ldM loops per second\n", (unsigned long) micro);
Ollie Lhocbbf1252004-03-17 22:22:08 +000037}