how to handle cpu cache in python ( or fastest way to call a function once)

Stefan Behnel stefan_ml at behnel.de
Sun Aug 23 05:54:21 EDT 2015


Yuzhi Xu schrieb am 23.08.2015 um 08:10:
> I find out that python's VM seems to be very unfriendly with CPU-Cache.
> see:
> http://stackoverflow.com/questions/32163585/how-to-handle-cpu-cache-in-python-or-fastest-way-to-call-a-function-once
> http://stackoverflow.com/questions/32153178/python-functionor-a-code-block-runs-much-slower-with-a-time-interval-in-a-loop
> 
> for example:
> *******************************************
> import time
> a = range(500)
> 
> sum(a)
> 
> for i in range(1000000): #just to create a time interval, seems this disturb cpu cache?
>     pass
> 
> 
> st = time.time()
> sum(a)
> print (time.time() - st)*1e6
> 
> *********************************************
> time:> 100us
> 
> 
> another case:
> *********************************************
> import time
> a = range(500)
> 
> for i in range(100000):
>     st = time.time()
>     sum(a)
>     print (time.time() - st)*1e6
> 
> *********************************************
> time:~ 20us
> 
> 
> we can see when running frequently, the code becomes much faster.

That does not seem like a straight forward deduction. Especially the
interpretation that the CPU caching behaviour is to blame here seems rather
far fetched.

My guess is that it rather has to do with CPython's internal object caching
or something at that level. However, given the absolute timings above, I
wouldn't bother too much finding it out. It's unlikely to hurt real-world
code. (And in fact, the more interesting case where things are happing
several times in a row rather than being a negligible constant one-time
effort seems to be substantially faster in your timings. Congratulations!)


> is there a solution?

Is there a problem?

Stefan





More information about the Python-list mailing list