Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does struct.pack have such high variability in performance?

I get the following result when testing the performance of struct.pack:

In [3]: %timeit pack('dddd', 1.0, 1.0, 1.0, 1.0)
The slowest run took 578.59 times longer than the fastest. This could
mean that an intermediate result is being cached 
1000000 loops, best of 3: 197 ns per loop

Why is the slowest run 578 times slower? Is pack doing some internal caching, or is this the result of some sort of CPU-level caching, or something else?

like image 251
Thomas Johnson Avatar asked Mar 30 '15 16:03

Thomas Johnson


1 Answers

The IPython profiler is spot on. The result is indeed cached (at least in some python versions). For example in python 2.7.6 you can find the relevant code here where the cache_struct function is defined.

This function looks up the cache to see if, recently, the given format was used and returns the relevant Struct instance instead of creating a new one (which seems relatively expensive).

You can see that it is used in the pack function (and others).


This content was posted in comments but deserves to be an answer.

like image 162
Bakuriu Avatar answered Oct 12 '22 16:10

Bakuriu