Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Decreasing CPU usage with sync off

Author
Message
Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 22nd Jun 2011 20:19
I have read few threads about decreasing CPU usage, and found a solution- to turn off sync. As i did (commented out sync function) my program started to blink and fps decreased from 40 to 10.

Newcomers question:
What you use instead of sync?

Never seen a 3D program example without "sync on" :/

Join The dark Side! We have cookies
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 22nd Jun 2011 20:37
If you turn sync off, then you need to take SYNC out of your main loop. Turning sync off just means that DBP is doing the syncing for you, and if I recall it's automatically set to 60 fps.

With SYNC ON you have more control over how often the sync actually occurs.

Have a look at Decoupling the Display Loop from the Game Loop for better game performance

It doesn't show you how to release unneeded CPU cycles, but it does show you how to make use of all the CPU you can to maintain a steady framerate.

In the game loop you can do things like use the command NICE WAIT from the Matrix1Utils, and execute NICE WAIT X every Y loops to give some CPU time back if your program can spare it.

For example, if your program's Display loop is running at a steady 60 LPS, and your Game loop is running at 3,000 LPS, then in the Game loop you could put,



Every 10th loop of the Game loop would release 1 millisecond of CPU time back to the system. You would be surprised how quickly that adds up, and you might have to write a dynamic routine to adjust that wait value or change it to every 20th loop so it doesn't slow you down to much when the load gets heavy.

It's basically all about dynamically directing the power where it needs to be at the time.

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 23rd Jun 2011 13:09
That sounds like a good answer, ty
I will find out more about Timer Based Movement. It does look kinda good new path to my program to go, instead of built-in sync rate commands

Join The dark Side! We have cookies
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Jun 2011 19:57
A simpler way is to use the command 'SYNC SLEEP 1' - this instructs DBPro to do the equivalent of my NICE SLEEP command within the SYNC command. If you use it, you'll find that your processor usage drops right down.

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 24th Jun 2011 14:35
Definitely check out Sync Sleep. I've tested it successfully in some of my applications (some times the frame rate drops too much though). It's very easy to figure out too, just a little trial and error whilst Task Manager is running and keep monitoring your CPU compared to your apps FPS.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 24th Jun 2011 15:15
Yes, works for me perfectly. CPU usage in my menu is now negligible, and the menu still performs ok.

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 24th Jun 2011 15:51
"sync sleep 1" did dropped CPU usage from 100 to 80-90 percent

But i don't understand its meaning...

In
http://dbprohelp.wikia.com/wiki/SYNC_SLEEP
there is an example of code to give 500 ms for processor to rest.

If function is logical type (0-1) so it would work on any integer value bigger than 0. Else as sleep value is increased, it should drop CPU down more and more... but it doesn't! If i set more than 1, CPU is 100% again

Join The dark Side! We have cookies
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 24th Jun 2011 17:17
If you are only doing this at a menu, then you can put in the menu main loop something that monitors the frame rate. If the frame rate is high enough, then change a variable to reflect how much time you want to give back to Windows.

if FPS > 50 then WaitTime = 1
if FPS < 30 then WaitTime = 0
NICE WAIT WaitTime

Should give you the results you want at the menu. You can play around with it and see what works best. Maybe perform this check only once a second.



My suggestion about Decoupling the Display and Game loops is best for the main game loop when there's a lot more going on.

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 24th Jun 2011 19:09
No not only a menu. I aim at main loop. Will try combo sleeping and decoupling after someone explain (or say it isn't explainable- magic or smth) why sleeping only work on value 1

Join The dark Side! We have cookies
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 24th Jun 2011 21:26
It could be a bug, or a documentation error. If it works as an ON/OFF with 0 and 1 values, then it's probably a documentation error.

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 24th Jun 2011 21:56
But if it is ON/OFF so it would work on any other values like 5, 100 250, 500 and so on... At least thats how C++ works. Doesn't DBP understand any positive value as TRUE? Does it work on bigger values than 1 for someone?

Join The dark Side! We have cookies
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 25th Jun 2011 00:50
I use NICE WAIT 20 and normal SYNC when the player is in the menu and the mouse has stopped moving for 10 seconds. This reduces CPU on the game to 0% (in Task Manger). Obviously it uses some CPU time, but not enough to register.

The menu also wakes up very fast, 20 milliseconds is not enough for a user to notice it coming back to life when the mouse moves. All screen animation is also smooth.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 25th Jun 2011 00:56 Edited at: 25th Jun 2011 00:56
For games I personally think one of the best solutions is to simply enable v-sync, which from my recollection forces the program to (nicely) wait until the frame can be drawn.



Support a charitable indie game project!
Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 25th Jun 2011 10:52
originally 39-51 FPS and 100% CPU load (sync rate set to 60)

VSync = 1 and
Set Display Mode PixelsX,PixelsY,32,VSync
30-33 FPS and average of 90% CPU

sync sleep 1
average 38 FPS and 80-90% CPU

both of them
33-37 FPS and average of 66% CPU (though it jumps a lot 58-75%)
It is strange but when firefox is active window, CPU usage grows to 80+ and when click on my app, it drops back down

Join The dark Side! We have cookies
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 13th Jul 2011 22:37
I've experienced some strange CPU load discrepancies in my program as well.

For example, the MMORPG that I am currently working on is built in three parts: The client, the server, and the world editor. The world editor and the client do not experience the CPU discrepancy that the server does, even though all three programs use the same system to draw to the screen (SYNC OFF, SYNC SLEEP 1, one SYNC per draw iteration):

When the server is first booted, it uses up a slice of CPU, as expected of any program. However, when it's minimised and then restored to usual size, it's suddenly eating significantly less CPU, and still performing at an identical level.

All I did was resize the window, and I see a CPU usage change during execution.

I realise this isn't an answer to your question, just some additional information that might help you pinpoint what causing it. It's not a big enough problem for me to warrant further investigation, but I'll keep an eye on your thread to see if anyone figures it out

Login to post a reply

Server time is: 2024-11-22 18:23:48
Your offset time is: 2024-11-22 18:23:48