Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95584

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

From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: how to handle cpu cache in python ( or fastest way to call a function once)
Date 2015-08-23 11:54 +0200
References <2aa39ddd-bb07-4a09-a046-a011e215882a@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.32.1440323670.17298.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to handle cpu cache in python ( or fastest way to call a function once) Yuzhi Xu <yuzhixu.ruc@gmail.com> - 2015-08-22 23:10 -0700
  Re: how to handle cpu cache in python ( or fastest way to call a function once) Stefan Behnel <stefan_ml@behnel.de> - 2015-08-23 11:54 +0200
  Re: how to handle cpu cache in python ( or fastest way to call a function once) Steven D'Aprano <steve@pearwood.info> - 2015-08-23 21:59 +1000
  Re: how to handle cpu cache in python ( or fastest way to call a function once) Vladimir Ignatov <kmisoft@gmail.com> - 2015-08-23 08:07 -0400
    Re: how to handle cpu cache in python ( or fastest way to call a function once) Steven D'Aprano <steve@pearwood.info> - 2015-08-23 22:42 +1000

csiph-web