Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80834
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Peter Pearson <pkpearson@nowhere.invalid> |
| Newsgroups | comp.lang.python |
| Subject | Re: Strange result with timeit execution time measurment |
| Date | 15 Nov 2014 18:12:52 GMT |
| Lines | 36 |
| Message-ID | <ccpjd4Ff1m9U1@mid.individual.net> (permalink) |
| References | <54678857$0$2325$426a74cc@news.free.fr> |
| X-Trace | individual.net Gf+z8GvQF/mKM3zbkwzZEwcTKXShYfHVxu1XlSfOBtqEGMuWKH |
| Cancel-Lock | sha1:EkCORs2EBYw7dTGLoDo/mfEt45c= |
| User-Agent | slrn/pre1.0.0-18 (Linux) |
| Xref | csiph.com comp.lang.python:80834 |
Show key headers only | View raw
On Sat, 15 Nov 2014 18:07:30 +0100, ast wrote:
>
> 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 ?
I suppose math.sin is implemented in C. Compiled languages (like C) are
much faster than interpreted languages like Python.
--
To email me, substitute nowhere->runbox, invalid->com.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Strange result with timeit execution time measurment "ast" <nomail@invalid.com> - 2014-11-15 18:07 +0100 Re: Strange result with timeit execution time measurment Peter Pearson <pkpearson@nowhere.invalid> - 2014-11-15 18:12 +0000 Re:Strange result with timeit execution time measurment Dave Angel <davea@davea.name> - 2014-11-15 13:08 -0500 Re: Strange result with timeit execution time measurment Peter Otten <__peter__@web.de> - 2014-11-15 18:59 +0100 Re: Strange result with timeit execution time measurment Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-15 11:18 -0700
csiph-web