blob: ad2fb4e3c5820cd8a53ee73fd5b3d9ef595291b3 [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 Lhocec28792004-03-17 23:03:37 +000010 for (i = 0; i < time * micro; i++)
Ollie Lhocbbf1252004-03-17 22:22:08 +000011 ;
12}
13
14void myusec_calibrate_delay()
15{
16 int count = 1000;
17 unsigned long timeusec;
18 struct timeval start, end;
19 int ok = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +000020
21 printf("Setting up microsecond timing loop\n");
22 while (!ok) {
Ollie Lhocbbf1252004-03-17 22:22:08 +000023 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 Lhocbbf1252004-03-17 22:22:08 +000028 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 Lhocbbf1252004-03-17 22:22:08 +000038}