00:00
00:00
Newgrounds Background Image Theme

Allthingz2020 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

I've always wondered this. AS3

344 Views | 4 Replies
New Topic Respond to this Topic

I've always wondered this. AS3 2015-10-11 15:38:38


Is there a performance(or efficiency) difference between these two examples:

//update function

   if(a == 2){
      this.gotoAndStop(2);
   }

vs

//update function

   if(a == 2 && this.currentFrame != 2){
      this.gotoAndStop(2);
   }

Both examples would be tied to an ENTER_FRAME event. In example 1, the block is always setting the frame to 2, but example 2 only sets the frame to 2 if the condition is met and if it is not already at 2.

Is there a difference between constantly applying an effect and only doing it once if?

Response to I've always wondered this. AS3 2015-10-11 15:54:11


At 10/11/15 03:38 PM, Barzona wrote: Is there a difference between constantly applying an effect and only doing it once if?

Sometimes. gotoAndStop might have internal logic that already checks if the current frame is equal to the parameter. Or it might not. You can always use getTimer() to time your code to see for yourself.

Response to I've always wondered this. AS3 2015-10-11 18:42:07


At 10/11/15 03:54 PM, MSGhero wrote:
At 10/11/15 03:38 PM, Barzona wrote: Is there a difference between constantly applying an effect and only doing it once if?
Sometimes. gotoAndStop might have internal logic that already checks if the current frame is equal to the parameter. Or it might not. You can always use getTimer() to time your code to see for yourself.

Good to know, though me using a gotoAndStop() was just an example. What about continuously casting another type of data? If I was constantly making a string = "Word" vs only making that string = "Word" if it didn't already?

Response to I've always wondered this. AS3 2015-10-11 18:59:05


At 10/11/15 06:42 PM, Barzona wrote: Good to know, though me using a gotoAndStop() was just an example. What about continuously casting another type of data? If I was constantly making a string = "Word" vs only making that string = "Word" if it didn't already?

That's so minor I'm not even sure it makes a difference. For something like that, you should pick the option that makes sense when you read the code rather than shaving a nanosecond off your enterframe loop. There is no reason to keep setting it to the same value each frame, regardless of what function you're calling or what datatype it is.

I'm assuming you have some score Int that you're putting into a string. Once you've updated the score, then I would change the string, rather than checking every frame to see if it needs to be changed. It depends, though.

Response to I've always wondered this. AS3 2015-10-12 18:08:57


In 1974, Knuth wrote:

"The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming."

qft

At 10/11/15 03:38 PM, Barzona wrote: Is there a performance(or efficiency) difference between these two examples:

//update function

if(a == 2){
this.gotoAndStop(2);
}

vs

//update function

if(a == 2 && this.currentFrame != 2){
this.gotoAndStop(2);
}

Both examples would be tied to an ENTER_FRAME event.

Why does it run on that Event?
The condition will essentially only change if the variable a changes.
Provide a public setter function for a and evaluate the received value there.