Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (1 invisible), 735 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Whats faster? Check if key is pressed or calculate every frame #356293
02/01/11 00:06
02/01/11 00:06
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
The common maybe professional way for using key inputs seems to be something like that:

Code:
pXent_addforcecentral(ball, vector((key_cur-key_cul)*0.1, 0, 0));



I wonder if the more clearly "newby" way...

Code:
if (key_cul) pXent_addforcecentral(ball, vector(-0.1, 0, 0));
if (key_cur) pXent_addforcecentral(ball, vector(0.1, 0, 0));



...is not probably even faster? Isn't it easier for the engine checking if the key is pressed every frame intead of calculating every time addforcecentral?

Of course it also depends on the command which is used for the calculating... So what's the best rule of thumb?

Last edited by Clemens; 02/01/11 09:49.
Re: Whats faster? Check if key is pressed or calculate every frame [Re: Clemens] #356311
02/01/11 05:42
02/01/11 05:42
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
the best rule of thumb is to not worry about such stuff, write readable code and only optimize the bottlenecks. laugh

pXent_addvelcentral() doesn't do much. it just changes the velocity of a body. it's like vec_add().
edit: it's the same with pXent_addforcecentral().

(your examples don't do the same thing if you press both buttons at the same time.)

Re: Whats faster? Check if key is pressed or calculate every frame [Re: ventilator] #356320
02/01/11 09:49
02/01/11 09:49
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
Ups, you're right, has to be the force command of course (changed it)...

So you mean, if there is an effect then it is in such a way minimal that it can be uncared-for developing ?

I just think, if there are many parallel functions like this in the end ... the sum will make a difference to the faster code version !?!

Re: Whats faster? Check if key is pressed or calculate every frame [Re: Clemens] #356321
02/01/11 10:16
02/01/11 10:16
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
The first method is of course faster in terms of CPU cycles because of two reasons:
1) The CPU doesn't have to check two times if a variable evaluates to true
2) The CPU only needs to call the function pXent_addforcecentral once, instead of two times. Calling functions isn't completely inexpensive as you first need to push the variables on the stack and then jump to a completely different memory region to continue the execution. Jumping back to the caller needs to pop a few variables from the stack and copy the result into eax, also not completely inexpensive

That said, it doesn't matter which way you choose as the time isn't measurable for a human (for really, really, high precision timers its measurable, but your brain will fail this challenge).


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: Whats faster? Check if key is pressed or calculate every frame [Re: WretchedSid] #356352
02/01/11 14:47
02/01/11 14:47
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
Quote:

2) The CPU only needs to call the function pXent_addforcecentral once, instead of two times.

Hm, but the function is only called, if the key is pressed, isn't it!?! So normally only one key is pressed ... and all times when none of the keys is pressed, it has to call the function not at all.

Quote:

That said, it doesn't matter which way you choose as the time isn't measurable for a human (for really, really, high precision timers its measurable, but your brain will fail this challenge).

-> my argument of "in sum" (à la "Die Summe macht's!") [haha, three languages in one sentence tongue ] !?

Re: Whats faster? Check if key is pressed or calculate every frame [Re: Clemens] #356353
02/01/11 14:56
02/01/11 14:56
Joined: Oct 2007
Posts: 5,210
Ä°stanbul, Turkey
Quad Online
Senior Expert
Quad  Online
Senior Expert

Joined: Oct 2007
Posts: 5,210
Ä°stanbul, Turkey
go with:

Code:
if(key_cur || key_cul)pXent_addforcecentral(ball, vector((key_cur-key_cul)*0.1, 0, 0));
89




3333333333
Re: Whats faster? Check if key is pressed or calculate every frame [Re: Quad] #356358
02/01/11 15:19
02/01/11 15:19
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
even in sum the difference would be extremely small. the bottlenecks will be somewhere else.

or do you have to call the function ten or hundred thousand times per frame? you will first get problems because of other things then (entity count,...).

Re: Whats faster? Check if key is pressed or calculate every frame [Re: Quad] #356362
02/01/11 15:47
02/01/11 15:47
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
Originally Posted By: Quadraxas
go with:

Code:
if(key_cur || key_cul)pXent_addforcecentral(ball, vector((key_cur-key_cul)*0.1, 0, 0));
89



Why should that be faster? Should be similar to
Code:
if(key_cur)pXent_addforcecentral(ball, vector((key_cur-key_cul)*0.1, 0, 0));
if(key_cul)pXent_addforcecentral(ball, vector((key_cur-key_cul)*0.1, 0, 0));


and that would need more calculating than the second code version I posted.


Originally Posted By: "ventilator"

or do you have to call the function ten or hundred thousand times per frame?

That's a good point wink
The discussion is just focus on the question of the best and fastest coding algorithm you can do in general.
...cause if you have a really long program code with a lot of whiles it could become important if you use all the time the fast or slow method. Even if you use commands which are marked with "speed: slow" like c_move!

Re: Whats faster? Check if key is pressed or calculate every frame [Re: Clemens] #356374
02/01/11 16:42
02/01/11 16:42
Joined: Jan 2011
Posts: 120
United States
Logan Offline
Member
Logan  Offline
Member

Joined: Jan 2011
Posts: 120
United States
I like Quadraxas' code... unlike your second code with the two ifs, his pXent_addforcecentral is called a maximum of once even if both keys are pressed, whereas yours could be called twice if both keys were pressed.

I largely agree with ventilator though. When was the last time you experienced a slowdown because of too many ifs in a function? Or too many vec_sets? Worry about 65 entites who while(1) c_trace 5 times per frame.

Re: Whats faster? Check if key is pressed or calculate every frame [Re: Logan] #356384
02/01/11 17:24
02/01/11 17:24
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
Okay, but it normally makes no sense to press both keys - so this case will happen nearly never or only for a really short moment when the user switch the direction (key pressing).


Okay, imagine you have hundreds of characters, only moved by c_move if they can see you. Your two code options (both in a while) are:
Code:
if (CanSeePlayer) c_move(me, vector(7,0,0), nullvector, GLIDE);


or
Code:
c_move(me, vector(CanSeePlayer*7,0,0), nullvector, GLIDE);


... so, which one should be faster?
I guess the question is: Does c_move also cost "that much" CPU power when the result is that it doesn't move (like c_move(me, nullvector, nullvector, GLIDE)?



Last edited by Clemens; 02/01/11 17:25.
Page 1 of 2 1 2

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