Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Radiant on Tue 18/03/2008 09:11:23

Title: "if" and performance
Post by: Radiant on Tue 18/03/2008 09:11:23
I have a few question about if, because I'm using way too many ifs in my scripts these days.

If there are multiple conditions in an if (using && or ||) are these all always evaluated? Or does e.g. "if (x && y)" stop evaluating when x turns out to be false?

Are mathematical conditions simplified by AGS, or should I do that by hand? If I have CONST as a #defined (or an enum'ed) constant with a value of 5, is " if (x > CONST + 1) " the same as " if (x > 6) "? Or does the addition get performed every time the if statement is encountered?

Does it matter whether I do " if (x) { y } else { z } "  or " if (x) { y; return; } z " ?

Thanks!
Title: Re: "if" and performance
Post by: SSH on Tue 18/03/2008 10:15:10
Since 2.71, AGS has had lazy evaluation. Dunno about the others. Why not set up some examples in a game, run them 10000 times and time it?
Title: Re: "if" and performance
Post by: cat on Tue 18/03/2008 11:48:59
Quote from: Radiant on Tue 18/03/2008 09:11:23
Does it matter whether I do " if (x) { y } else { z } "  or " if (x) { y; return; } z " ?

I'd suggest not to use return statements for flowcontrol. Chances are high that you might miss them when you change something in your code and the code wont work correctly.
That's why many coding guidelines don't allow return statements inside a function that are not the last statement.
Title: Re: "if" and performance
Post by: Radiant on Tue 18/03/2008 11:59:06
QuoteSince 2.71, AGS has had lazy evaluation.
Great, thanks.

Quote from: SSH on Tue 18/03/2008 10:15:10
Why not set up some examples in a game, run them 10000 times and time it?
Because that wouldn't give me the answer to several of these.

Quote from: cat on Tue 18/03/2008 11:48:59
I'd suggest not to use return statements for flowcontrol.
I'm talking about practical optimization here, not theoretical coding standards. That's because my project is actually slowing down because of script complexity.
Title: Re: "if" and performance
Post by: SSH on Tue 18/03/2008 12:15:47
One thing to consider is whether all your stuff needs to really occur in one game cycle. For example: if you're doing something which affects a character, but the character has a delay of 4 cycles on its walking animation, then you could script it so that you only calculate 1/4 of your stuff per game cycle...

Title: Re: "if" and performance
Post by: SSH on Tue 18/03/2008 12:52:11
a took 132s
b took 129s
c took 171s
d took 180s

so using a constant was slightly slower..
and NOT using an else was slightly slower...



#define ACONST 45
#define LOOPS 500000
function testa(int foo) {
  if (foo < (ACONST * 2) ) {
    return(7);
  } else {
    return(11);
  }
}

function testb(int foo) {
  if (foo < 90 ) {
    return(7);
  } else {
    return(11);
  }
}

function testc(int foo) {
  int f=20;
  if (foo < 90 ) {
    f=f-13;
    return f;
  }
  f=f-9;
  return f;
}

function testd(int foo) {
  int f=20;
  if (foo < 90 ) {
    f=f-13;
    return f;
  } else {
    f=f-9;
    return f;
  }
}




function noloopcheck room_AfterFadeIn()
{
  SetMultitaskingMode(1);
  int i;
  i=0;

  DateTime *dtprea=DateTime.Now;
  i=0;
  while (i<LOOPS) {
    int j=0;
    int sum=0;
    while (j<180) {
      sum=sum+testa(j);
      j=j+1;
    }
    i=i+1;
  }
  DateTime *dtposta=DateTime.Now;
 
  DateTime *dtpreb=DateTime.Now;
  i=0;
  while (i<LOOPS) {
    int j=0;
    int sum=0;
    while (j<180) {
      sum=sum+testb(j);
      j=j+1;
    }
    i=i+1;
  }
  DateTime *dtpostb=DateTime.Now;

  DateTime *dtprec=DateTime.Now;
  i=0;
  while (i<LOOPS) {
    int j=0;
    int sum=0;
    while (j<180) {
      sum=sum+testc(j);
      j=j+1;
    }
    i=i+1;
  }
  DateTime *dtpostc=DateTime.Now;

  DateTime *dtpred=DateTime.Now;
  i=0;
  while (i<LOOPS) {
    int j=0;
    int sum=0;
    while (j<180) {
      sum=sum+testd(j);
      j=j+1;
    }
    i=i+1;
  }
  DateTime *dtpostd=DateTime.Now;
  Display("testa * %d took %d seconds", LOOPS, dtposta.RawTime-dtprea.RawTime);
  Display("testb * %d took %d seconds", LOOPS, dtpostb.RawTime-dtpreb.RawTime);
  Display("testc * %d took %d seconds", LOOPS, dtpostc.RawTime-dtprec.RawTime);
  Display("testd * %d took %d seconds", LOOPS, dtpostd.RawTime-dtpred.RawTime);

}

Title: Re: "if" and performance
Post by: Radiant on Wed 19/03/2008 16:32:56
Cool, thanks!

(your statistics seem to indicate that using an else is slightly slower)