Jump to content
  • Advertisement
Sign in to follow this  
sixteen

do something once per second? how?

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how do I make something happen once per second? I currently have:
#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;
}
but it makes the CPU usage 100%. The counter doesn't really delay time like I thought it would.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
You should use cstdio instead of stdio.h, stdio.h is a C header, not C++.

Share this post


Link to post
Share on other sites
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:


#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!

:)

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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);
}
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!