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

Yuzhi Xu yuzhixu.ruc at gmail.com
Sun Aug 23 02:10:55 EDT 2015


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.

is there a solution?


I feel this question is very difficult. one must has indepth unstanding about the mechanism of python virtual machine, c and cpu-cache.

Do you have any suggestion about where to post this question for a possible answer?



More information about the Python-list mailing list