Is there, as in JavaScript, a performance penalty for creating functions in C? -
in javascript, there are, often, huge performance penalties writing functions. example, if utilize function:
function double(x){ homecoming x*2; }
inside inner loop, hitting performance considerably, profitable inline kind of function intensive applications. this, in general, hold c? free create kind of functions everything, , rest assured compiler job, or hand inlining still important?
the reply is: depends.
i'm using msvc compiler , gcc project @ work , experience both pretty job. furthermore, cost of function phone call in native code can pretty small, in functions not need accessible outside executable (like functions not exported in shared library). these functions, there more flexibility how phone call implemented.
a few things note: it's much easier compiler optimize calls static functions. functions external linkage require link time optimization since 1 must know how , function called, implementation, much optimization or inlining. requires examining more 1 compilation unit @ time.
i should utilize functions makes sense , makes code easier read , maintain. in general, safe assume cost smaller in javascript. in end, you'd have profile code more precise.
update: want emphasize functions can inlined across compilation units, requires link-time optimization (or whole programme optimization). supported in both gcc (https://gcc.gnu.org/wiki/linktimeoptimization) , msvc (http://msdn.microsoft.com/en-us/library/0zza0de8.aspx).
these days, if can beat compiler copying body of function , pasting everywhere phone call function, need different compiler.
c performance optimization
No comments:
Post a Comment