This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dalvengyr
Dalvengyr wrote ...

2014/11/25

Function call?

Dalvengyr Dalvengyr

2014/11/25

#
Well, I'm very new to java scripting, so probably I will have bunch of questions and need to understand which is good and bad. As far as I see, most people here are used to separate every certain pack of actions into a separated function (method, in java). Whereas I'm used to believe that function call is always slower. For code readability, it's very good to construct your code that way, but for speed-wise perspective, it's obviously no good. I have also read some references in C programming that function call is also slower, I wonder if it happens the same in java scripting :)
danpost danpost

2014/11/25

#
From a CPU perspective, yes -- method calls take longer to execute than direct code. The elimination of the long call drops the internal instructions to add a long address to the stack, change the stack and instruction pointer and the control segment address, and then the opposite when returning from the call. However, with the speed of computers today, it has become less of an issue and emphasis of coding for optimal performance in this regard has greatly declined. When it comes to Greenfoot, if you maintain a moderate speed (speed slider near the middle), unless you really lax on performance issues, there is usually ample time to spare after an act frame is completed.
davmac davmac

2014/11/25

#
danpost is right; putting it bluntly, it's extremely unlikely that would ever have a noticable speed difference between code broken up into separate functions and equivalent code without the functions. On the other hand, the code that's broken up into functions is much more readable. This is also true in C, especially when using a modern compiler with optimizations turned on.
Dalvengyr Dalvengyr

2014/11/25

#
How about calling getWorld() over and over again? Is that an excessive process? Currently I'm still optimizing codes of my tower game because it get quite lagged after reached level ~60s. I hope I can make it completely flawless by removing as many function call as possible. Thanks for your answer guys.
davmac davmac

2014/11/25

#
How about calling getWorld() over and over again? Is that an excessive process?
No, as I've said, it's unlikely that you'll see a measurable cost to function calls.
You need to login to post a reply.