#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
int counter = 0;
while(true){
counter += 1;
if(counter >= 20000)
{
cout << "done" << endl;
remove( "downloads.bak" );
remove( "downloads.dat" );
}
}
return 0;
}
do something once per second? how?
how do I make something happen once per second? I currently have:
but it makes the CPU usage 100%. The counter doesn't really delay time like I thought it would.
Hey bud,
Look up the function Sleep(), the s might be lower case. I think you need windows.h for it.
Hope that helps bud.
Dave
Look up the function Sleep(), the s might be lower case. I think you need windows.h for it.
Hope that helps bud.
Dave
Your implementation relies on how long it takes the computer to process a 20000 part loop, so on faster processors it will be done in less time.
The best way to do it is to:
What this will do is check the current number of clock cycles since the program started, dividing this by CLOCKS_PER_SEC you will get the number of seconds that have passed. Then, each time a new second passes, bam!
:)
The best way to do it is to:
#include <ctime>using namespace std;void main(){ int numSecondsPassed = 0; while(true) { if(clock() / CLOCKS_PER_SEC > numSecondsPassed) { ++numSecondsPassed; //PUT HERE WHATEVER CODE HAPPENS EVERY SECOND } }}
What this will do is check the current number of clock cycles since the program started, dividing this by CLOCKS_PER_SEC you will get the number of seconds that have passed. Then, each time a new second passes, bam!
:)
I have some wonderful timing code at home that I will be more than happy to give you. It takes into account the speed of the processor, using a function called QueryPeformanceFrequency().
Quote:Original post by Moe
I have some wonderful timing code at home that I will be more than happy to give you. It takes into account the speed of the processor, using a function called QueryPeformanceFrequency().
That's what the above clip I posted does as well :)
Quote:Original post by JBourrie
int numSecondsPassed = 0;
while(true)
{
if(clock() / CLOCKS_PER_SEC > numSecondsPassed)
{
++numSecondsPassed;
//PUT HERE WHATEVER CODE HAPPENS EVERY SECOND
}
}
That code will eat a lot of CPU time.
As Dave already suggested, use Sleep.
thanks everyone. I ended up going the Sleep() route because the other ways still made the CPU go to 100%
#include <cstdio>
#include <windows.h>
using namespace std;
int main(){
while(true)
{
remove( "downloads.bak" );
remove( "downloads.dat" );
Sleep(1000);
}
}
Yes, timing like this is (for lack of a better term) retarded. It just doesn't work on modern multitasking operating systems. It also doesn't work on machines of a different speed, machines under heavy load already, etc. I remember doing such things on my 386 in MSDOS, but there was no other way to get really accurate timing then (60th of a second, not one second). Just use sleep().
Oh, and if you're really unlucky and try to use a loop like that for timing, the compiler will just optimize it away and you'll get zero timing :P
Oh, and if you're really unlucky and try to use a loop like that for timing, the compiler will just optimize it away and you'll get zero timing :P
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement