Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
4 registered members (AndrewAMD, fogman, Grant, juanex), 972 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
WHILE versus IF : Slow and buggy code? #324635
05/22/10 12:25
05/22/10 12:25
Joined: Apr 2007
Posts: 125
Brazil - São Paulo
Ericmor Offline OP
Member
Ericmor  Offline OP
Member

Joined: Apr 2007
Posts: 125
Brazil - São Paulo
I have been experimenting weird behaviors all around my entities, and while cleaning up my old code (he's from 2004, OLD syntaxes and everthing), i found this:

Code:
while(arrow_1.tilt<= -81)
{arrow_1.tilt = -80;waitt(1);}
 while(arrow_1.tilt>= 45)
{arrow_1.tilt = 44;waitt(1);}
 while(Camera_in_block==1&&flecha_1.tilt<0)
{arrow_1.tilt -= -10*time_frame;wait(1);}
 while(Camera_in_block==1&&flecha_1.tilt>1)
{arrow_1.tilt -=  10*time_frame;wait(1);}



Yup, four 'WHILES' inside a function. Maybe in the old engine that might be necessary, but what now? Wich of the following functions is faster: One 'while' inside a function, or one 'while' inside the WHOLE SCRIPT, calling other functions?

Code:
fuction firstfunction()
{ 
 while(1)
 { 
  if(arrow_1.tilt<= -81)
 {arrow_1.tilt = -80;waitt(1);}
  if(arrow_1.tilt>= 45)
 {arrow_1.tilt = 44;waitt(1);}
  if(Camera_in_block==1&&flecha_1.tilt<0)
 {arrow_1.tilt -= -10*time_frame;wait(1);}
  if(Camera_in_block==1&&flecha_1.tilt>1)
 {arrow_1.tilt -=  10*time_frame;wait(1);}
  }
wait(1);
}

fuction secondfunction()
{ 
 if(movement!=1)
 { 
 if(arrow_1.tilt<= -81)
{arrow_1.tilt = -80;waitt(1);}
 if(arrow_1.tilt>= 45)
{arrow_1.tilt = 44;waitt(1);}
 if(Camera_in_block==1&&flecha_1.tilt<0)
{arrow_1.tilt -= -10*time_frame;wait(1);}
 if(Camera_in_block==1&&flecha_1.tilt>1)
{arrow_1.tilt -=  10*time_frame;wait(1);}
 }
wait(1);
}

function functioncaller()
{
 while(1)
 {
 secondfunction();
 thirdfunction();
 //...
 wait(1);
 }
}




Thanks in advance.

Re: WHILE versus IF : Slow and buggy code? [Re: Ericmor] #324643
05/22/10 13:15
05/22/10 13:15
Joined: Feb 2010
Posts: 68
mireazma Offline
Junior Member
mireazma  Offline
Junior Member

Joined: Feb 2010
Posts: 68
If we let apart the typos, 'while' is faster than calling a function. So put 4 'while' in a function.
But even so, excuse me for suggesting (sorry if I don't understand what you want to do) -- why do you use 'while' instead of 'if' in the first snippet?
Further, in 'firstfunction' definition, wait(1) in the 'if' blocks -- you want to skip the following 'if' checks? Cause you won't.
In 'secondfunction' definition, using wait(1) in a one time 'if' statement instead of a loop is suspicious.
I think (not sure) that 'while' and 'if' are of the same speed when using them for the same purpose, with the remark that in the 'while' you have to use wait(1) which is further consuming.

Last edited by mireazma; 05/22/10 13:18.

ERROR in communism.cpp, line 0:
#include<god.h>
was fatally missed.
Re: WHILE versus IF : Slow and buggy code? [Re: mireazma] #324645
05/22/10 13:23
05/22/10 13:23
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
I suggest going with your "firstfunction()",
but get rid of the apparently spare "wait()"s inside some of the if's,
and just have a single one inside the loop at its end.




"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: WHILE versus IF : Slow and buggy code? [Re: EvilSOB] #324667
05/22/10 15:07
05/22/10 15:07
Joined: Apr 2007
Posts: 125
Brazil - São Paulo
Ericmor Offline OP
Member
Ericmor  Offline OP
Member

Joined: Apr 2007
Posts: 125
Brazil - São Paulo
I learned, right in the beggining, that 'while' instructions tak up more processing than 'if' ones.
In any structure logic, one single 'while' loop calling several functions with 'if' and 'else' statements would take the same amount of time as several 'while' inside various functions, simply because the engine will take a simple frame cicle trought all instructions, and the only difference is that the engine will keep repetaing the 'while', but not the 'if'.
All i wanted to know is if there is a difference in SPEED between several 'while' against only one 'while' and several 'if', but because of the engine structure, NOT code logic.
About the wait(1): remember, this is WDL code and all 'while' need a 'wait(1)' at the end.
let me try being more clear:
Which following function is faster? 'Firstfunction()', or 'functioncaller()'?

Code:
Function Firstfunction()
{
 While(1){
 item_handle.roll +=1*time_step; //rotates item_handle
 wait(1);
 }
 While(item_handle.skill1 != 0)
 {
 item_handle.pan +=1*time_step; //rotates item_handle
 wait(1);
 }

 While(item_handle.skill1 == 2)
 {
 item_handle.tilt +=1*time_step; //rotates item_handle
 wait(1);
 }

 While(item_handle.skill1 == 3)
 {
 item_handle.x +=1*time_step; //moves item_handle
 wait(1);
 }
}



Second option:

Code:
Function Secondfunction()
{
 if(item_handle.skill1 != 0)
 {
 item_handle.pan +=1*time_step; //rotates item_handle
 }

 if(item_handle.skill1 == 2)
 {
 item_handle.tilt +=1*time_step; //rotates item_handle
 }

 if(item_handle.skill1 == 3)
 {
 item_handle.x +=1*time_step; //moves item_handle
 }
wait(1);
}

Function function_caller
{
 while(1)
 {
 Secondfunction();
 Thirdfunction();
 Fourthfunction();
 wait(1);
 }
}











Last edited by Ericmor; 05/22/10 15:09. Reason: logic.
Re: WHILE versus IF : Slow and buggy code? [Re: Ericmor] #324673
05/22/10 15:42
05/22/10 15:42
Joined: Sep 2002
Posts: 1,604
Deutschland
ChrisB Offline
Serious User
ChrisB  Offline
Serious User

Joined: Sep 2002
Posts: 1,604
Deutschland
The first one, as it would never ever leave the first while loop.
In case you don't know them, here are the 3 laws of optimization:
1. Don't do it.
2. Don't do it.
and only for advanced programmers:
3. Don't do it yet.

Seriously these kind of optimization are senseless in a normal game. Your game will be hardly ever slowed downed because of your game logic. You should optimize your code to readability or reuse ability instead.
If you still want an answer:
You can't really compare if, loops and function calls, as they do different things, and your two example doesn't do the same thing.
A while(cond) is speed wise similar to a single if, it is basically a conditional branch. A while(1) instead is a bit faster as it is only a simple jump (good for the cpu instruction pipeline).
A function call is something completely different. It copies the return address of the current function to the stack and then jumps to the new function address. Its still very fast.
On the other side all these information are useless if you use wait(1); as this would call the scheduler of the acknex engine, which cost way more than anything mentioned before.

For example you should change your first example from:
Code:
if(arrow_1.tilt<= -81)
 {arrow_1.tilt = -80;waitt(1);}
  if(arrow_1.tilt>= 45)
 {arrow_1.tilt = 44;waitt(1);}


to
Code:
arrow_1.tilt = clamp(arrow_1.tilt, -80, 44);


It's easier to read, to understand, its less code and it is even faster.


www.Swollen-Eyeballs.org
ICQ:169213431
#3dgs@quakenet
Re: WHILE versus IF : Slow and buggy code? [Re: ChrisB] #324674
05/22/10 15:55
05/22/10 15:55
Joined: Apr 2007
Posts: 125
Brazil - São Paulo
Ericmor Offline OP
Member
Ericmor  Offline OP
Member

Joined: Apr 2007
Posts: 125
Brazil - São Paulo
WOW... instruction i didn't heard before!
Code:
clamp(var x,var a,var b);



Good one, ChrisB! I KNEW i could optimize those bunch of 'while's somehow.
Thanks for the explanation!


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