1 registered members (AndrewAMD),
1,244
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
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
OP
Senior Member
|
OP
Senior Member
Joined: Sep 2003
Posts: 303
Germany
|
The common maybe professional way for using key inputs seems to be something like that:
pXent_addforcecentral(ball, vector((key_cur-key_cul)*0.1, 0, 0));
I wonder if the more clearly "newby" way...
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
Senior Expert
|
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. 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: Clemens]
#356321
02/01/11 10:16
02/01/11 10:16
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
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
OP
Senior Member
|
OP
Senior Member
Joined: Sep 2003
Posts: 303
Germany
|
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. 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 ] !?
|
|
|
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
Senior Expert
|
Senior Expert
Joined: Oct 2007
Posts: 5,210
Ä°stanbul, Turkey
|
go with:
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]
#356362
02/01/11 15:47
02/01/11 15:47
|
Joined: Sep 2003
Posts: 303 Germany
Clemens
OP
Senior Member
|
OP
Senior Member
Joined: Sep 2003
Posts: 303
Germany
|
go with:
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
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. or do you have to call the function ten or hundred thousand times per frame?
That's a good point 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: Logan]
#356384
02/01/11 17:24
02/01/11 17:24
|
Joined: Sep 2003
Posts: 303 Germany
Clemens
OP
Senior Member
|
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:
if (CanSeePlayer) c_move(me, vector(7,0,0), nullvector, GLIDE);
or
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.
|
|
|
|