Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; 'cpython': 0.05; 'pypy': 0.07; 'explanation': 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; "'from": 0.16; '10000000': 0.16; '100000000': 0.16; 'ast': 0.16; 'interesting:': 0.16; 'isnt': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'test.': 0.24; 'math': 0.24; 'looks': 0.24; '(or': 0.24; 'compare': 0.26; 'switch': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'code': 0.31; '>>>>': 0.31; 'constant': 0.31; 'invoke': 0.31; 'overhead': 0.31; 'subject:time': 0.33; 'subject:with': 0.35; 'test': 0.35; 'but': 0.35; 'skip:- 20': 0.37; 'needed': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:t 30': 0.61; 'simple': 0.61; 'results': 0.69; 'applying': 0.72; 'genuine': 0.78; 'faster.': 0.84; 'optimisation': 0.84; 'sinus': 0.84; 'universe': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Strange result with timeit execution time measurment Date: Sat, 15 Nov 2014 18:59:22 +0100 Organization: None References: <54678857$0$2325$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd87d0.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416075286 news.xs4all.nl 2891 [2001:888:2000:d::a6]:49925 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80836 ast wrote: > Hi > > I needed a function f(x) which looks like sinus(2pi.x) but faster. > I wrote this one: > > -------------------------- > from math import floor > > def sinusLite(x): > x = x - floor(x) > return -16*(x-0.25)**2 + 1 if x < 0.5 else 16*(x-0.75)**2 - 1 > -------------------------- > > then i used module timeit to compare its execution time with math.sin() > I put the sinusLite() function in a module named test. > > then: > >>>> import timeit >>>> t1 = timeit.Timer("y=test.sinusLite(0.7)", "import test") >>>> t2 = timeit.Timer("y=math.sin(4.39)", "import math") ## 4.39 = >>>> 2*pi*0.7 > >>>> t1.repeat(3, 1000000) > [1.9994622221539373, 1.9020670224846867, 1.9191573230675942] > >>>> t2.repeat(3, 1000000) > [0.2913627989031511, 0.2755561810230347, 0.2755186762562971] > > so the genuine sinus is much faster than my so simple sinLite() ! > Amazing isnt it ? Do you have an explanation ? You are applying your optimisation in an implementation where the function call overhead of a Python-implemented function is greater than the time to invoke the C-coded function, calculate the sin, and create the python float. $ python -m timeit -s 'from math import sin' 'sin(.7)' 1000000 loops, best of 3: 0.188 usec per loop $ python -m timeit -s 'from test import sinusLite as sin' 'sin(.7)' 1000000 loops, best of 3: 0.972 usec per loop $ python -m timeit -s 'sin = lambda x: None' 'sin(.7)' 1000000 loops, best of 3: 0.242 usec per loop For CPython to write fast lowlevel code you have to switch to C (or Cython). In PyPy the results get interesting: $ pypy -m timeit -s 'from test import sinusLite as sin' 'sin(.7)' 100000000 loops, best of 3: 0.00459 usec per loop $ pypy -m timeit -s 'from math import sin' 'sin(.7)' 10000000 loops, best of 3: 0.0476 usec per loop So yes, your approximation may speed up code in some parts of the Python universe (I don't know if pypy takes advantage of the constant argument).