Jump to content

Recommended Posts

hi

whats better ?

method 1:

while { true }
{
 if (myVariable > 0) then
 {
   do something....
 };

 sleep 1;
};

method 2:

processMyVar = false;

if (myVariable > 0) then
{
 processMyVar = true;
};

while { true }
{
 if (processMyVar ) then
 {
   do something....
 };

 sleep 1;
};

sure.. method 2 creates more variables (so using more memory) but my question is... will it save processor time ?

Share this post


Link to post
Share on other sites

For starters the second method makes no sense as when you start the loop processMyVar will always be either false or true depending on what it was when you entered loop. Also trying to optimise script for speed that runs 60 times a minute is total waste of time IMO

Share this post


Link to post
Share on other sites
For starters the second method makes no sense as when you start the loop processMyVar will always be either false or true depending on what it was when you entered loop. Also trying to optimise script for speed that runs 60 times a minute is total waste of time IMO

its example and i thought that i will get theoretical answer.. :(

limitScore = false;
limitTime = false;

if (scoreLimit > 0) then
{
 limitScore = true;
};

if (timeLimit > 0) then
{
 limitTime = true;
};

while { true }
{
 if (limitScore) then
 {
   if (scoreLimit..........) then
   {
     do something....
   };
 };

 if (limitTime) then
 {
   if (timeLimit ..........) then
   {
     do something....
   };
 };

 sleep 1;
};

its based on that i know that limitScore and limitTime will never change inside the loop.... is it theoretical faster ?

Share this post


Link to post
Share on other sites

Those two examples in your first post don't even function the same way. No since trying to figure out if one is faster or anything.

I don't think you quite understand how those would run.

In your first example your while true block would begin to run in a loop and each cycle it would execute the if statement.

Your second example the first if statement would execute one time. Then the while true loop would begin to run and it would execute the if statement inside the while loop each cycle.

The two algorythims don't function the same so you can't compare them.

Like another poster said when you start running functions hundreds or thousands of times a frame or your running hundreds of loops. You probably don't need to worry about it. But there is nothing wrong with learning about it.

Share this post


Link to post
Share on other sites
Those two examples in your first post don't even function the same way. No since trying to figure out if one is faster or anything.

I don't think you quite understand how those would run.

In your first example your while true block would begin to run in a loop and each cycle it would execute the if statement.

Your second example the first if statement would execute one time. Then the while true loop would begin to run and it would execute the if statement inside the while loop each cycle.

The two algorythims don't function the same so you can't compare them.

Like another poster said when you start running functions hundreds or thousands of times a frame or your running hundreds of loops. You probably don't need to worry about it. But there is nothing wrong with learning about it.

huh ?

method 1:

while { true }
{
 if (myVariable > 0) then
 {
   do something....
 };

 sleep 1;
};

method 2:

while { true }
{
 if (processMyVar) then
 {
   do something....
 };

 sleep 1;
};

this is NOT same ?

Edited by LoonyWarrior

Share this post


Link to post
Share on other sites

Why don't you measure performance yourself by making each example execute 100000 times? Take time at the beginning and at the end then see which one does it faster. If you do go this way, make sure you write correct syntax for while command, it is while {} do {} not while {} {}

Share this post


Link to post
Share on other sites

when u have IF statement...

is fater

if ( true )

or its same as

if ( 1 > 0 )

....the true statement in first case is already done..

Share this post


Link to post
Share on other sites
huh ?

method 1:

while { true }
{
 if (myVariable > 0) then
 {
   do something....
 };

 sleep 1;
};

method 2:

while { true }
{
 if (processMyVar) then
 {
   do something....
 };

 sleep 1;
};

this is NOT same ?

Nope. Because in the second example, if processMyVar is evaluated outside the while loop, it will always have the same value when inside the while loop. It never gets a chance to change. It will either never {do something....} or it will always {do something....} depending on whether it was evaluated as false or true.

UNLESS the {do something....} actually changes processMyVar...

Share this post


Link to post
Share on other sites
Nope. Because in the second example, if processMyVar is evaluated outside the while loop, it will always have the same value when inside the while loop. It never gets a chance to change. It will either never {do something....} or it will always {do something....} depending on whether it was evaluated as false or true.

UNLESS the {do something....} actually changes processMyVar...

look at expample n. 2... thats the point...

and question is...

when u have IF statement...

is fater

if ( true )

or its same as

if ( 1 > 0 )

....the true statement in first case is already done..

Share this post


Link to post
Share on other sites

Unfortunately there is no simple Yes or No answer to your question because depending on what value is your myVariable and to what you compare it performance differs. As I said, test it yourself.

Share this post


Link to post
Share on other sites
Why don't you measure performance yourself by making each example execute 100000 times? Take time at the beginning and at the end then see which one does it faster. If you do go this way, make sure you write correct syntax for while command, it is while {} do {} not while {} {}

:)

myVariable1 = 0;
myVariable2 = time;
while { myVariable1 < 10000 } do
{
 if ( [b]STATEMENT[/b] ) then
 {
   titleText ["experiment", "PLAIN"];
 };

 myVariable1 = myVariable1 + 1;
};
myVariable3 = time;

if ( false )

0.306

0.304

0.301

0.303

if ( 0 > 0 )

0.308

0.311

0.309

0.308

Share this post


Link to post
Share on other sites
look at expample n. 2... thats the point...

and question is...

In this particular case I guess there may be a tiny imperceptible difference.

*edit*

I guess you measured it :)

Share this post


Link to post
Share on other sites
In this particular case I guess there may be a tiny imperceptible difference.

*edit*

I guess you measured it :)

lol and i was just thinking why u said that ....when i posted the reseults already.. ;)

Share this post


Link to post
Share on other sites

Your still not comparing anything In your last example.

To compare two functions you need a few things.

1. They need to do the same job.

-- ie how long would it take to spawn 100 opfor vs spawning 10 tanks.

2. They need to work at least slightly different.

-- ie. If I was trying to spawn 100 ai would it be faster to spawn 10 groups of 10 air or a 100 groups of 1 ai.

Each of these example accomplished the same thing the just did it in different ways. Then you can test to see which one is best.

Edited by Riouken

Share this post


Link to post
Share on other sites
Your still not comparing anything In your last example.

To compare two functions you need a few things.

1. They need to do the same job.

-- ie how long would it take to spawn 100 op for vs spawning 10 tanks.

2. They need to work at least slightly different.

-- ie. If I was trying to spawn 100 air would it be faster to spawn 10 groups of 10 air or a 100 groups of 1 air.

Each of these example accomplished the same thing the just did it in different ways. Then you can test to see which one is best.

The point isn't the loop content, that has to be identical. The point was the loop conditional only.

Share this post


Link to post
Share on other sites

Lol. Your not getting it. The engine does the same exact same thing in both your examples. The engine just checks if the statement is true or not. The only way you could see an impact is if you checked one single condition on one and multipul conditions on the other.

That doe sent even take into account your deviation in your results.

Your test for all intents and purposes is the exact same.

Share this post


Link to post
Share on other sites
Lol. Your not getting it. The engine does the same exact same thing in both your examples. The engine just checks if the statement is true or not.

This is true :)

But in one case the conditional does not have to be evaluated first, only compared to true. In the other, the conditional has to be evaluated first, then the result compared to true.

Share this post


Link to post
Share on other sites
Those two examples in your first post don't even function the same way. No since trying to figure out if one is faster or anything.

I don't think you quite understand how those would run.

In your first example your while true block would begin to run in a loop and each cycle it would execute the if statement.

Your second example the first if statement would execute one time. Then the while true loop would begin to run and it would execute the if statement inside the while loop each cycle.

The two algorythims don't function the same so you can't compare them.

Like another poster said when you start running functions hundreds or thousands of times a frame or your running hundreds of loops. You probably don't need to worry about it. But there is nothing wrong with learning about it.

Lol. Your not getting it. The engine does the same exact same thing in both your examples. The engine just checks if the statement is true or not. The only way you could see an impact is if you checked one single condition on one and multipul conditions on the other.

That doe sent even take into account your deviation in your results.

Your test for all intents and purposes is the exact same.

hups ? :j:

...im sorry but u know what.. ..we know what we re talking about.. do u ?

Edited by LoonyWarrior

Share this post


Link to post
Share on other sites
hi

whats better ?

method 1:

while { true }
{
 if (myVariable > 0) then
 {
   do something....
 };

 sleep 1;
};

method 2:

processMyVar = false;

if (myVariable > 0) then
{
 processMyVar = true;
};

while { true }
{
 if (processMyVar ) then
 {
   do something....
 };

 sleep 1;
};

sure.. method 2 creates more variables (so using more memory) but my question is... will it save processor time ?

hups ? :j:

...im sorry but u know what.. ..we know what we re talking about.. do u ?

Yes I do but i'm afraid you're missing the point.

There is no need to check an if statement you know it to be true... that is where the real processing time come from. There is no need to put an if statement in a loop unless you need to check the statement each iteration.

Method 2 could look something like this.

if (myVariable > 0) then
{

 while {true}
 {
    do something....


   sleep 1;
 };

};


processMyVar = false;


if (myVariable > 0) then
{
 processMyVar = true;
};


while { true }
{
 if (processMyVar ) then [color=#ff0000]// There is no need for this statement, why spend the time checking this if you already know it to be true in your if statement above. The only time you need to check would be if this var would be changing.[/color]
 {
   do something....
 };


 sleep 1;
};

Share this post


Link to post
Share on other sites
// There is no need for this statement, why spend the time checking this if you already know it to be true in your if statement above. The only time you need to check would be if this var would be changing.

= when user decide to limit score and time ...make two loops........ why two loops ?

Share this post


Link to post
Share on other sites
= when user decide to limit score and time ...make two loops........ why two loops ?

What two loops are you referring to?

You said in the beginning this was theoretical. If you have a real problem your trying to solve please tell us and we will be able to assist you better.

Share this post


Link to post
Share on other sites
its example and i thought that i will get theoretical answer.. :(

limitScore = false;
limitTime = false;

if (scoreLimit > 0) then
{
 limitScore = true;
};

if (timeLimit > 0) then
{
 limitTime = true;
};

while { true }
{
 if (limitScore) then
 {
   if (scoreLimit..........) then
   {
     do something....
   };
 };

 if (limitTime) then
 {
   if (timeLimit ..........) then
   {
     do something....
   };
 };

 sleep 1;
};

its based on that i know that limitScore and limitTime will never change inside the loop.... is it theoretical faster ?

What two loops are you referring to?

You said in the beginning this was theoretical. If you have a real problem your trying to solve please tell us and we will be able to assist you better.

theoretical because there is sleep command which slows that down..................

in fact... im afraid that you re missing the point....

there wasnt problem at all..

i had question about performance of IF statement in loop

question was...

is faster

if ( false )

or

if ( 0 > 0 )

i wasnt sure if engine will take true as true or it will try to figure out if true is true..

and what u re talking about ? personally... i have no idea...

...because while u was talking about apples we actually discovered that its faster to use boolean by itself.. ...case solved... (actually on previous page)

There is no need to put an if statement in a loop unless you need to check the statement each iteration.

// There is no need for this statement, why spend the time checking this if you already know it to be true in your if statement above. The only time you need to check would be if this var would be changing.

and when u wish to limit score and time for pvp mission.. and user can decide..

limit only score

limit only time

limit both of them

when user decide to limit both of them.... u say make two loops... and i ask.... why ?

u look at that only from one side... but it has two sides....

There is no need to put an if statement in a loop unless you need to check the statement each iteration = make two loops

Edited by LoonyWarrior

Share this post


Link to post
Share on other sites

And once again you do not understand at all, your question is WRONG, the whole idea, the way your coding etc...

It does not matter what you put in the if statement, as you can see from your test, the difference is imperceptible. What matters is that your completing the if statement. If you already have the var, or bool figured out, before the if statement (Like in your example A) then why do you need to complete the if Statement?

theoretical because there is sleep command which slows that down..................

in fact... im afraid that you re missing the point....

there wasnt problem at all..

i had question about performance of IF statement in loop

question was...

is faster

if ( false ) [color=#ff0000]// No need to do this at all. Why do you need to evaluate something that is hard coded that you already know. Why not just take out this whole step , and then it really will be faster than the option below.[/color]

or

if ( 0 > 0 )

i wasnt sure if engine will take true as true or it will try to figure out if true is true..

---------- Post added at 08:47 PM ---------- Previous post was at 08:35 PM ----------

Let me explain it another way.

if (scoreLimit > 0) then
{
 limitScore = true;
};


if (timeLimit > 0) then
{
 limitTime = true;
};

[color=#ff0000]// Everything above this line only runs once in this script. And you set limitScore & limitTime based on the if statements above. This only happens once.[/color]



while { true }
{
 if (limitScore) then [color=#ff0000]// Next you are checking the above variable and if its true then you do stuff. But this is in a loop, and it will keep running over and over. There is no reason to spend this proc power on this unless limitScore will be changing. And
                          // if its going to be changing then your test is moot because at some point your going to have to evaluate limitScore so you can just do it here if that is the case. [/color]
 {
   if (scoreLimit..........) then
   {
     do something....
   };
 };


 if (limitTime) then
 {
   if (timeLimit ..........) then
   {
     do something....
   };
 };


 sleep 1;
};


Share this post


Link to post
Share on other sites

:cool:

Everything above this line only runs once in this script. And you set limitScore & limitTime based on the if statements above. This only happens once. Next you are checking the above variable and if its true then you do stuff. But this is in a loop, and it will keep running over and over

hmm 5 years php 2 years c# and u re telling me that it do what its supposed to do ? ...wow ....thank you

if (scoreLimit > 0) then
{
 limitScore = true;
};


if (timeLimit > 0) then
{
 limitTime = true;
};

[color=#ff0000]// Everything above this line only runs once in this script. And you set limitScore & limitTime based on the if statements above. This only happens once.[/color]



while { true }
{
 if (limitScore) then [color=#ff0000]// Next you are checking the above variable and if its true then you do stuff. But this is in a loop, and it will keep running over and over. There is no reason to spend this proc power on this unless limitScore will be changing. And
                          // if its going to be changing then your test is moot because at some point your going to have to evaluate limitScore so you can just do it here if that is the case. [/color]
 {
   if (scoreLimit..........) then
   {
     do something....
   };
 };


 if (limitTime) then
 {
   if (timeLimit ..........) then
   {
     do something....
   };
 };


 sleep 1;
};


when user decide to limit both score and time.... what u will do ? ....if u will stick with that u will end up with another loop... and prove me that not... so... why two loops ?

u re simply ignoring what the code says.. its hard then......

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×