views:

80

answers:

5

Does calling more functions have any kind of noticeable effect of performance, or is trying to cut down on number of function calls a pointless premature optimization?

Simplified Example: Is there any real performance example between this:

function foo($var) {
    echo $var;
}
foo();

And This:

function foo($var) {
   bar($var);
}

function bar($var) {
  baz($var);
}

function baz($var) {
  echo $var;
}

foo();
+6  A: 

Always do profiling first, then bother with optimizations.

On the other hand, function calls do have some overhead, but you should really profile your code before you decide to optimize out function calls.

PeterK
+5  A: 

It's premature optimization. You should have functions that make sense based on your domain and the logical separation of tasks in your code. The only time I would ever consider replacing a function with inline code is if it was in a tight loop and a profiler showed it was a problem.

To more directly answer your question, the effect on performance hinges on quite a few things:

  1. How often is the function called?
  2. Is it in a critical section of the code?
  3. How large is/are the function(s)?

Deciding on whether something should be in a function should be driven by logical separation, clarity, maintainability, etc. and not performance.

RC
A: 

The overhead of a function call is pretty stiff actually. But it's still low in the grand scheme of things. If you're only calling the chain a handful of times in the execution, don't worry about it. If you're going to be looping a lot, then you may want to consider optimizing it a little bit.

But remember, the biggest performance gain is the transition from a non-working state to a working one. Only then start tackling the actual bottlenecks... And never forget: Premature optimization is the root of all evil...

ircmaxell
A: 

I wouldn't worry about speed so much as about memory, since each function call costs stack. Again, worrying about this in examples like yours would be premature optimization, but it's good to have it in mind if you're planning for a recursive algorithm in a language that's not optimized to handle recursive function calls.

Nicolas78
+1  A: 

To answer your question in the simplest way No.

Now for the longer answer, function overhead is so small that you won't begin to notice it until you've made at least several million calls on a particular function. In most UI programs that very rarely happens having millions of calls to a method. What's inside your function is going to contribute vastly more to your performance than the overhead associated with calling the function.

To further illustrate my point. Computers operate on the nanosecond timeframe. Humans can detect about 10 milliseconds (from UI testing). 1ms = 1.0 × 10^6 ns. So if it takes 1ns to call your function you'd need to call it between 1 million to 10 million times before a human could tell the difference.

If an function's contents is expensive to call then reducing when you call it can help performance, but again that has everything to do with what's inside the function and NOT the act of calling the function.

Never try and optimize before you've measured it. I'll say it again never never try and predict what will be performant in your application. Always measure performance with a profiler. It's like trying to predict the bugs in your program before you've written it.

chubbard