Remember to mention those function parameters!

Jan 16, 2006   //   by Tuomas Artman   //  1 Comment

Funny how things (Flash) work. I was optimising an application when I stumbled upon this oddity:

Take the following function:

function test():Number{
    return 1;
}

Be stupid and call it in a for-loop 50.000 times and record the time it takes to execute: 230 ms.

Now add some parameters to the function:

function test(a,b,c,d,e,f,g,h,i,j,k):Number{
    return 1;
}

Running the same test (without actually passing in any parameters to the function) reveals that execution takes about 1100 ms. While this is odd (we don’t actually pass in any parameters or use those parameters in any way), it’s still explainable – maybe Flash reserves some memory for the params every time the function gets called.

But try explaining this: If you "mention" all the parameters that get passed to the function…

function test(a,b,c,d,e,f,g,h,i,j,k):Number{
    return 1;
    a; b; c; d; e; f; g; h; i; j; k;
}

execution time drops again to 230 ms ;) Err…

I was trying to optimise my code and found it a little confusing that when I commented some code out of a function that was called very frequently everything actually got slower…

1 Comment

  • Hehe, pretty odd how flash works. I’m still trying to learn it myself to make games..but I am only good at AS2 and AS3 is hard for me to learn.

Leave a comment