Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (TipmyPip, AndrewAMD, Quad, aliswee, degenerate_762), 970 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
experimental snippet for less cpu usage if it's not needed #464650
03/03/17 17:45
03/03/17 17:45
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline OP
Expert
Kartoffel  Offline OP
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Here's an experimental code snippet (global while-loop) that's supposed minimize acknex's CPU-usage if it's not needed.
However, for this to work you have to use a global while loop and execute everything from there ( which is probably not the way you're used to do things smirk ).

Also, note that I'm not even sure if this works properly.

Code:
#include <acknex.h>
#include <windows.h>

//

void main()
{
	fps_max = 60;
	
	//
	
	double dTms = 0.0; // delta time (milliseconds)
	
	dtimer();
	wait(1);
	
	while(1)
	{
		proc_mode = PROC_LATE;
		
		dTms += dtimer() / 1000.0; // dTms = last frame's elapsed time in milliseconds
		
		//////////
		
		
		
		
		//// do EVERYTHING from here
		
		
		
		
		//////////
		
		double dTms_busy = dtimer() / 1000.0; // time 
		dTms = dTms_busy;
		
		int SleepTime = floor(1000.0 / fps_max - dTms_busy - 2.0); // remaining time in milliseconds (rounded down, minus 2 for safety)
		
		if(SleepTime > floor(1000.0 / fps_max) - 2) // limit max. sleep time
			SleepTime = floor(1000.0 / fps_max) - 2;
		
		if(SleepTime > 0) // use window's Sleep() function if necessary
			Sleep(SleepTime);
		
		//
		
		wait(1);
	}
}


I read somewhere that acknex's frame timer syncs up the program with the screen refreshrate by "wasting" cpu cycles until the end of the frame is reached and the program can continue.
this has no impact on how fast the program runs since it only does that after all the code for that specific frame is executed but it's still an unecessary way of keeping the cpu usage up.

in the code above I'm using dtimer() to measure the actual execution time of the instructions (within one frame) and calculate the remaining time until the next frame can start.
then I'm using window's Sleep() function to get closer to the end of the frame so acknex's internal timer has less time to wait (should be around 1-3 milliseconds).

Last edited by Kartoffel; 03/03/17 18:07. Reason: small code change

POTATO-MAN saves the day! - Random
Re: experimental snippet for less cpu usage if it's not needed [Re: Kartoffel] #464653
03/03/17 21:13
03/03/17 21:13
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline
Serious User
Reconnoiter  Offline
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Interesting. What I currently do for a project is limiting fps_max (to e.g. 10) after some idle time and/or no acknex is not the top window. On my laptop this reduces cpu usage in the task manager from ~35% to 3% (and estimated battery time from ~1.5 to ~3.5).


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1