Steveng
Steveng

Reputation: 31

function calling performance

I have called snprintf a few times consecutively with different arguments. I take the time needed for each snprintf. I found that the first call to snprintf takes the longest time. After that, the time needed to call the same function decreases until it converges. What is the reason for that? I have tried with other functions and also exhibit the same behavior.

I am asking this because it relates to testing the code performance. Normally in the main program it would be only be called periodically. However, when I test the function separately like in a loop, it would be faster, hence, resulting in inaccuracy of the measurement of performance.

The first call takes 4000++ ns, second call takes 1700ns, third call takes 800 ns until around 10++ calls, it is reduced to 130ns.

snprintf(buffer, 32, "%d", randomGeneratedNumber1);
snprintf(buffer, 32, "%d", randomGeneratedNumber2);
   .
   .
   .

Upvotes: 3

Views: 200

Answers (3)

Amir Zadeh
Amir Zadeh

Reputation: 3629

Search TLB and cache. But for just small answer, in these small codes, cache effects the execution time. For large codes, besides the cache, many memory pages will be swapped out and for later usage swapped in from hard disk to your ram. So when you use a part of code very often it will not be swapped out and thus it's execution time is enhanced.

Upvotes: 1

mouviciel
mouviciel

Reputation: 67829

Your program may be dynamically linked to the library containing snprintf(). The first time delay would then be what is needed to load the library.

Upvotes: 2

EboMike
EboMike

Reputation: 77752

The most likely explanation is that both the function code will end up in the instruction cache after the second time around just like the input data (if there is any) will be in the data cache. Furthermore, some branches may be predicted correctly the second time around.

So, all in all, "things have been cached".

Upvotes: 3

Related Questions