LightTab2 LightTab2 - 1 year ago 110
C Question

What is void function(void) impact on performance?

It is said that this kind of function helps in making a program more readable, does it mean it has no or almost no impact on performance? I think it creates a scope, but does it affect runtime performance? Thanks in advance.


void function(void){

int main()
{ //c++
//rawcodeSameAsInFunction //"faster"?
function(); //"slower"?
return 0;

Answer Source

Depends on optimization. If the compiler determines it can be inlined, there is zero overhead. Otherwise there is the overhead of a function call, and it depends on the context whether that is significant or not. You can check the assembler output of your compiler to see what happens in specific cases.

To help the compiler, you should make the function static (i.e., not exported). You can also try to request inlining by adding inline, but it may in fact be better to let the compiler decide.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download