Krupski
March 17th, 2009, 12:35 AM
Hi all,
I was playing around with the time() function just to learn how it works. I made up a tiny program called "pause.c" that works, but when run it kicks all 4 cpus (Intel Core 2 Quad) up to full speed!!
What the heck?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
long int time1, time2;
int delay;
// look for one command line param
if(argc != 2) {
fprintf(stderr, "usage: pause time (in milliseconds)\n");
return 1;
}
// get desired delay
delay = atoi(argv[1]);
// get start time
time2=time1=clock();
// while end-start < desired...
while((time2) - (time1) < (delay * (CLOCKS_PER_SEC / 1000))) {
time2=clock();
}
return 0;
}
Here's a dump of the average of 4 cores while the pause program was running (1 sample per second, pause ran for 10 seconds):
time ghz
------ -----
000000, 1.596
000001, 1.596
000002, 1.729
000003, 1.596
000004, 1.729
000005, 1.596
000006, 1.596
000007, 1.596
000008, 1.729
000009, 1.596
000010, 2.660 <---- start
000011, 2.660
000012, 2.660
000013, 2.660
000014, 2.660
000015, 2.660
000016, 2.660
000017, 2.660
000018, 2.660
000019, 2.660
000020, 2.660 <---- end
000021, 1.729
000022, 1.596
000023, 1.596
000024, 1.596
000025, 1.596
000026, 1.596
000027, 1.596
Anyone know why this uses all the CPU power?
Thanks!
-- Roger
I was playing around with the time() function just to learn how it works. I made up a tiny program called "pause.c" that works, but when run it kicks all 4 cpus (Intel Core 2 Quad) up to full speed!!
What the heck?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
long int time1, time2;
int delay;
// look for one command line param
if(argc != 2) {
fprintf(stderr, "usage: pause time (in milliseconds)\n");
return 1;
}
// get desired delay
delay = atoi(argv[1]);
// get start time
time2=time1=clock();
// while end-start < desired...
while((time2) - (time1) < (delay * (CLOCKS_PER_SEC / 1000))) {
time2=clock();
}
return 0;
}
Here's a dump of the average of 4 cores while the pause program was running (1 sample per second, pause ran for 10 seconds):
time ghz
------ -----
000000, 1.596
000001, 1.596
000002, 1.729
000003, 1.596
000004, 1.729
000005, 1.596
000006, 1.596
000007, 1.596
000008, 1.729
000009, 1.596
000010, 2.660 <---- start
000011, 2.660
000012, 2.660
000013, 2.660
000014, 2.660
000015, 2.660
000016, 2.660
000017, 2.660
000018, 2.660
000019, 2.660
000020, 2.660 <---- end
000021, 1.729
000022, 1.596
000023, 1.596
000024, 1.596
000025, 1.596
000026, 1.596
000027, 1.596
Anyone know why this uses all the CPU power?
Thanks!
-- Roger