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)

Path csiph.com!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'problem?': 0.07; 'interval,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'example:': 0.10; 'subject:python': 0.14; 'subject: \n ': 0.15; 'blame': 0.16; 'caching': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'row': 0.16; 'subject:fastest': 0.16; 'time.time()': 0.16; "wouldn't": 0.16; 'stefan': 0.18; 'constant': 0.22; 'see:': 0.22; 'pass': 0.22; 'code.': 0.23; 'seems': 0.23; 'absolute': 0.23; "python's": 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'behaviour': 0.29; 'cpu': 0.29; 'unlikely': 0.29; 'print': 0.30; 'code': 0.30; 'becomes': 0.30; 'guess': 0.31; 'another': 0.32; 'especially': 0.32; 'running': 0.34; 'something': 0.35; 'too': 0.36; 'there': 0.36; '(and': 0.36; 'faster': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'seem': 0.37; 'things': 0.38; 'several': 0.38; 'does': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'your': 0.60; 'above,': 0.63; 'more': 0.63; 'times': 0.63; 'one-time': 0.66; 'real-world': 0.66; 'here': 0.66; 'forward': 0.66; 'fact,': 0.67; 'faster.': 0.84; 'subject: ( ': 0.84; 'subject:handle': 0.84; 'timings': 0.84; 'disturb': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
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 Sun, 23 Aug 2015 11:54:21 +0200
References <2aa39ddd-bb07-4a09-a046-a011e215882a@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host ipservice-092-211-032-095.092.211.pools.vodafone-ip.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
In-Reply-To <2aa39ddd-bb07-4a09-a046-a011e215882a@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.32.1440323670.17298.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1440323670 news.xs4all.nl 23827 [2001:888:2000:d::a6]:50602
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95584

Show key headers only | 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