Gamestudio Links
Zorro Links
Newest Posts
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
folder management functions
by 7th_zorro. 04/15/24 10:10
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (Volkovstudio, 7th_zorro, Aku_Aku), 336 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
Reducing Function Times #481187
08/12/20 01:23
08/12/20 01:23
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
I just became aware that the biggest factor slowing my game is the function speed.
I assume reducing the number of lines in any given function should help to minimize this.

Do you have any general tips on reducing function complexity?

Re: Reducing Function Times [Re: Dooley] #481188
08/12/20 01:27
08/12/20 01:27
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
As an example, if I am running my game, it will get around 30 fps. If I pause the game (freeze_mode) it jumps up to 80 fps. Obviously there should be some difference, but it would be great to improve the efficiency of what I'm doing. According to the debug menu, functions are taking around 25 ms/frame. When paused it drops down to 6 or so.

Re: Reducing Function Times [Re: Dooley] #481189
08/12/20 06:22
08/12/20 06:22
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Hey!

I guess there are lots of things which can affect function times slower. But for me the biggest slowdown was using while loops for each entity (npc, items, objects, etc). How many while loops do you have in your game running at once? As a workaround, I would suggest to stop using while loops for main game logic at all. Instead, I would suggest using 'ENABLE_FRAME' for decor objects and on_frame for main game loop. I got this from one of the Superku's posts around the forum, this will help you with structuring function execution, as well will help to avoid using 'wait' (it sucks and can cause lots of troubles) and also will help to avoid using lots of while loops. If you need more examples, take a look at the source code of my Retro-FPS which is published on forum and can be downloaded from my github.

Greets.


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Reducing Function Times [Re: Dooley] #481190
08/12/20 10:11
08/12/20 10:11
Joined: Feb 2005
Posts: 48
Hamburg, Germany
Jerome8911 Offline
Newbie
Jerome8911  Offline
Newbie

Joined: Feb 2005
Posts: 48
Hamburg, Germany
Hi Dooley,

Quote
Do you have any general tips on reducing function complexity?

I'm dealing with the same issues, since a RTS gots tons of functions with loops.

For analyzing which part of the functions took most time to calculate I used dtimer() and displayed the time on screen.

Code
void* gameloop()
{
	...

	dtimer();
	check_unit_integrity();
	time_unit_loop = dtimer();
	
	dtimer();
	levelloop();
	time_level_loop = dtimer();	

	...
}

function show_info_txt()
{
	STRING* s1 = " ";

	...

	str_for_num(s1, time_unit_loop);
	draw_text("Rechenzeit Unitloop", 10, 275, WHITE);
	draw_text(s1, 140, 275, WHITE);

	str_for_num(s1, time_units);
	draw_text("Rechenzeit Einheiten", 10, 315, WHITE);
	draw_text(s1, 140, 315, WHITE);

	...
}


Searching structures took most of the execution time, so I would start from there. At start I even used ent_next to search for specific entities, obviously that takes a lot of time. Saving specific objects (like the units) in its own search lists saved a lot of time for me:
Code
//search list for units
int no_of_units;
int max_units = 256;
UNIT_STRUCT unit[256];

//search list for buildings
int no_of_buildings = 0;
int max_buildings = 64;
ENTITY* building_array[64];

...

Another thing you might consider is to "update" some functions less frequently (not every frame, but every other frame or even less frequent).

Best regards

Last edited by Jerome8911; 08/12/20 10:13.

Visit my indieDB page for Tactics of World War One

Or download Scheherazade's Journey, made for the A8 Winter 2020 Game Jam

Re: Reducing Function Times [Re: Dooley] #481193
08/12/20 14:47
08/12/20 14:47
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
These are great suggestions, thank you!

Wow, I was not even aware of ENABLE_FRAME so I will have to look into that. Lots of while loops in my code!

Re: Reducing Function Times [Re: Dooley] #481194
08/12/20 14:49
08/12/20 14:49
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
I also use ent_next, but I only use it prior to the entity's main loop. So will use it once at the beginning of a particular level, and it will assign the found entity to a ENTITY* pointer, for use later in the level...

dtimer() seems like it might be very useful in helping me find the bottlenecks, so I will check that out.

Re: Reducing Function Times [Re: Dooley] #481197
08/12/20 16:51
08/12/20 16:51
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
As for the ent_next, I would try to avoid cycling through all entities, I think this might be slow too (on big levels). Let's say you have 1000 entities on the level (sprites, models, etc) and you only need 100 of them, so instead of cycling through all 1000 and finding those that 100 you need, you can right at the start of the level, add those 100 into ENTITY array list (don't forget to have integer counter, to register total amount of entities) and then cycle within that list.
Code
ENTITY *list[999];

int counter = 0;

action add_to_list()
{
	list[counter] = my;
	counter++;
}

void cycle_in_list()
{
	int i = 0;
	for(i = 0; i < counter - 1; i++)
	{
		if(list[i] == NULL)
		{
			continue;
		}
		
		// do something with the entity
		
	}
}


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Reducing Function Times [Re: Dooley] #481198
08/12/20 17:50
08/12/20 17:50
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
I've always wondered about using for loops vs while loops. I generally use while loops as they just make more sense to me. But maybe a for loop could be more efficient when it does not have to wait for each frame?

Re: Reducing Function Times [Re: Dooley] #481199
08/12/20 17:57
08/12/20 17:57
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Well, as far as I know if you cycle fixed amount of times in while loop, it doesn't need to have wait either. I mostly use for loops since they are mainly made and used for that kind of purpose.


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Reducing Function Times [Re: 3run] #481200
08/12/20 20:19
08/12/20 20:19
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
I just switched one of the major loops from a while (without wait) to a for loop. It did not make any noticeable impact on frame rate.

Page 1 of 3 1 2 3

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