tags:

views:

152

answers:

5

I have a section of my program that contains a large amount of math with some rather long equations. Its long and unsightly and I wish to replace it with a function. However, chunk of code is used an extreme number of times in my code and also requires a lot of variables to be initialized.

If I'm worried about speed, is the cost of calling the function and initializing the variables negligible here or should i stick to directly coding it in each time?

Thanks,

-Faken

+12  A: 

Most compilers are smart about inlining reasonably small functions to avoid the overhead of a function call. For functions big enough that the compiler won't inline them, the overhead for the call is probably a very small fraction of the total execution time.

Check your compiler documentation to understand it's specific approach. Some older compilers required or could benefit from hints that a function is a candidate for inlining.

Either way, stick with functions and keep your code clean.

Eric J.
Ok, thank you. Ill do that from now on.
Faken
and never forget. profile profile profile! You can never guess exactly where bottle necks are coming from, and could waste time on places that you think are slow but aren't really.
Chris H
You can further increase the chances for the call to be inlined by using the inline directive (rather a "hint"...), and also some command line options which may prevent or encourage some performance options. Remember that regardless of inline hint and/or command line options, the compiler may still decide not to obey this hint, and remember our insight into profiling issues is shaky at best, until we measure things precisely; luckily most modern compilers are rather good at what they do.
mjv
+3  A: 

Are you asking if you should optimize prematurely?

Code it in a maintainable manner first; if you then find that this section is a bottleneck in the overall program, worry about tuning it at that point.

Mark Rushakoff
A: 

Modern C++ compilers generally inline small functions to avoid function call overhead. As far as the cost of variable initialization, one of the benefits of inlining is that it allows the compiler to perform additional optimizations at the call site. After performing inlining, if the compiler can prove that you don't need those extra variables, the copying will likely be eliminated. (I assume we're talking about primitives, not things with copy constructors.)

dsimcha
A: 

The only way to answer that is to test it. Without knowing more about the proposed function, nobody can really say whether the compiler can/will inline that code or not. This may/will also depend on the compiler and compiler flags you use. Depending on the compiler, if you find that it's really a problem, you may be able to use different flags, a pragma, etc., to force it to be generated inline even if it wouldn't be otherwise.

Without knowing how big the function would be, and/or how long it'll take to execute, it's impossible guess how much effect on speed it'll have if it isn't generated inline.

With both of those being unknown, none of us can really guess at how much effect moving the code into a function will have. There might be none, or little or huge.

Jerry Coffin
+2  A: 

You don't know where your bottlenecks are until you profile your code. Anything you can assume about your code hot spots is likely to be wrong. I remember once I wanted to optimize some computational code. I ran a profiler and it turned out that 70 % of the running time was spent zeroing arrays. Nobody would have guessed it by looking at the code.

So, first code clean, then run a profiler, then optimize the rough spots. Not earlier. If it's still slow, change algorithm.

Stefano Borini