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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With