LightTab2 LightTab2 - 3 months ago 20
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.

example:

void function(void){
//code
}

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

Answer

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.