Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any performance gain/loss with having several function calls rather than a single large one?

I am currently making a game for the iPad and iPhone using cocos2d, Box2D and Objective-C.

A lot of stuff is happening every update, and a lot has to be resolved.

I recently refactored a lot of my code to several small methods, instead of having hundreds of lines of code inside the same method.

Is there any performance loss doing this? Will fewer method calls increase performance?

like image 351
Simen Øian Gjermundsen Avatar asked Dec 07 '11 11:12

Simen Øian Gjermundsen


1 Answers

Each function call results in a constant-time (O(1)) delay because of the stack frame adjustments and branching. However, you won't feel that delay unless the calls are made inside a time-critical loop a million times.

The best approach would be, I think, writing the cleanest code possible and then optimizing it -- with the help of a profiler -- as needed.

You may also want to check out this answer: https://stackoverflow.com/a/4816703/252687 Inline functions may reduce the aforementioned overhead a bit without compromising the modularity.

like image 152
Eser Aygün Avatar answered Sep 27 '22 22:09

Eser Aygün