Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'python.': 0.02; 'skip:[ 20': 0.04; 'explanation': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'ast': 0.16; 'isnt': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'module': 0.19; 'written': 0.21; 'import': 0.22; 'test.': 0.24; 'math': 0.24; 'looks': 0.24; '15,': 0.26; 'compare': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'generally': 0.29; 'instruction': 0.29; 'message- id:@mail.gmail.com': 0.30; '>>>>': 0.31; 'implemented': 0.33; 'subject:time': 0.33; 'subject:with': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'doing': 0.36; 'skip:- 20': 0.37; 'nov': 0.38; 'needed': 0.38; 'to:addr :python-list': 0.38; 'skip:- 10': 0.38; 'to:addr:python.org': 0.39; 'even': 0.60; 'most': 0.60; 'skip:t 30': 0.61; 'simple': 0.61; 'series': 0.66; 'genuine': 0.78; 'faster.': 0.84; 'sinus': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=4E3zIIQTrL4xTXRQ3+b3M+8YsgfIPkHeMT661LwpUtg=; b=H05JgoB9DoHHB6FfnY873YTZp6xM+/XY9rrlBXdS2pk4P+AG7jt+L/w/JwUDdFk5fb Mlw/MivYy4HNUIwz/wCi4HO1/qnLqpfS57vtRwYfgpe5iMYXkiRyhQzEnRw7KgHo1fLS m2gGCBSWwetAuNNwGMkmQPKgD6hrOy4knnBrXX2O/XYcjgEswb4XkLt6kRmjiDynlJNB w/iNHJjehfpwG7sKFWokh6OyerjGr5YoUiGBFdYAtkXjqHU1nNjzy/Y6f4IApgHEkLpe smhVT2vO4hoqOPf1jwK5O+v0Q2Fau9EvP1Okwn1z/PGzR3NzQtUZlMG3NCYsujjMA9ah 7hVg== X-Received: by 10.67.4.197 with SMTP id cg5mr18778392pad.120.1416075521849; Sat, 15 Nov 2014 10:18:41 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <54678857$0$2325$426a74cc@news.free.fr> References: <54678857$0$2325$426a74cc@news.free.fr> From: Ian Kelly Date: Sat, 15 Nov 2014 11:18:01 -0700 Subject: Re: Strange result with timeit execution time measurment To: Python Content-Type: text/plain; charset=UTF-8 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416075531 news.xs4all.nl 2879 [2001:888:2000:d::a6]:57012 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80837 On Sat, Nov 15, 2014 at 10:07 AM, 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 ? The built-in sin is written in C, and the C implementation on most modern systems boils down to a single assembly instruction implemented in microcode. That's generally going to be faster than a whole series of operations written in Python. Even just doing the 2*pi multiplication in Python will add a lot to the timing: C:\>python -m timeit -s "import math" "math.sin(2*math.pi*0.7)" 1000000 loops, best of 3: 0.587 usec per loop C:\>python -m timeit -s "import math" "math.sin(4.39)" 1000000 loops, best of 3: 0.222 usec per loop