DuckQueen
DuckQueen

Reputation: 802

Is there a difference in terms of performance if a function accepts a ton of parameters or a single struct?

What is a principal difference in terms of performance (compiler optimizations such as inlining, L2 misses) if a function accepts a ton of parameters or a single struct?

example (used in some C code in Visual Studio)

void OnMessage(const char * senderId, const int & senderIdLength, const char * topic, const int & topicLength, const void * data, const int & dataLength);

Upvotes: 0

Views: 47

Answers (1)

Dweeberly
Dweeberly

Reputation: 4777

Every parameter needs to be stored somewhere (usually on the stack, or in a process register). Each of those stores requires some amount of time. Then there is the difference required to pass variables by value. If you have a struct of size 100 bytes and you pass the struct by reference you only need to pass the address (usually 4 or 8 bytes). If you pass by value you have to copy 100 bytes (to the stack). Compilers attempt to optimize stack and register usage, as best they can. L2 misses are effected most by the frequency a memory address is referenced over time. If your function is non-trivial you will likely spend much more time within it that calling it (cache usage will be largely determined by what the function does). If it's trivial then the compiler might very well in line it.

Upvotes: 1

Related Questions