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


Groups > comp.lang.python > #51004

Re: How can I make this piece of code even faster?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '(using': 0.07; '21,': 0.07; 'attribute': 0.07; 'python3': 0.07; 'subject:code': 0.07; 'subject:How': 0.10; 'python': 0.11; 'times,': 0.14; '"from': 0.16; '(same': 0.16; '10000000': 0.16; 'alpha,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'materially': 0.16; 'subject:make': 0.16; 'unlikely': 0.16; 'wrote:': 0.18; 'import': 0.22; 'adds': 0.24; 'math': 0.24; "i've": 0.25; 'suggested': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '-0700,': 0.31; "d'aprano": 0.31; 'faster,': 0.31; 'steven': 0.31; 'quite': 0.32; 'but': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'expensive': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'more': 0.64; 'jul': 0.74; '50%': 0.78; 'subject:this': 0.83; '3.4': 0.84; 'faster.': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=h4fpUkncZ9GabiSBMeyuMhKFnQBNkthD4XuDRvrsTZM=; b=xuJgRrW0TfkfHlPiTXsM5uYhbe4JcAselMmcFcUhmiZIk0s/uDqj0sTGBA3w9S3m1c DWasB9yRuNN9pWWv7DgOsdZKcHFBzlYo+FEUJkwjcsnIlmf/C7Easn8gd452zMjdsb6U fmdwn2HmupCS0ZPsBihBTStm2HCdya3YM5hqLvrvgZBNuRM8wHKRfiEwFoe82wuxB0CU r2cnwoOXsIvLb7fUErD4cfjP+DPyoRII7b7kFpuK5ew8i02KzyE7F0ZxdsaLI6c0ew+W tiY9ngKNlhzwhnvd0gDy/IRXuKWkuLRBasJm/WCP0MY6LIt07TgSrbqcb69LpVP4vaYh 8GtQ==
MIME-Version 1.0
X-Received by 10.52.93.106 with SMTP id ct10mr6606915vdb.83.1374403726095; Sun, 21 Jul 2013 03:48:46 -0700 (PDT)
In-Reply-To <51ebb88e$0$29971$c3e8da3$5496439d@news.astraweb.com>
References <6bf4d298-b425-4357-9c1a-192e6e6cd9f0@googlegroups.com> <9a207133-1f52-414a-bbbd-581bcee8dc93@googlegroups.com> <51ebb88e$0$29971$c3e8da3$5496439d@news.astraweb.com>
Date Sun, 21 Jul 2013 20:48:46 +1000
Subject Re: How can I make this piece of code even faster?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4947.1374403735.3114.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1374403735 news.xs4all.nl 15995 [2001:888:2000:d::a6]:45014
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51004

Show key headers only | View raw


On Sun, Jul 21, 2013 at 8:31 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Sun, 21 Jul 2013 03:19:24 -0700, pablobarhamalzas wrote:
>
>> Thank's for all the replies! I've tried some of the imporovements you
>> suggested (using math.exp() and sum() or math.fsum()). None of that made
>> the code faster, because they are functions you are calling lots of
>> times, and function calling is quite time expensive (same as x**(1/2) is
>> faster than math.sqrt(x)).
>
> You are *badly* mistaken. Not only is sqrt more accurate, but it is also
> much faster.
>
>
> [steve@ando ~]$ python3.3 -m timeit -s "x = 2.357e7" "x**0.5"
> 1000000 loops, best of 3: 0.319 usec per loop
> [steve@ando ~]$ python3.3 -m timeit -s "x = 2.357e7" -s "from math import
> sqrt" "sqrt(x)"
> 10000000 loops, best of 3: 0.172 usec per loop

Don't forget the cost of attribute lookup, which adds 50% to the
sqrt() figure. Still faster than exponentiation. (Figures from Python
3.4 alpha, but unlikely to be materially different.)

rosuav@sikorsky:~$ python3 -m timeit -s "x = 2.357e7" "x**0.5"
1000000 loops, best of 3: 0.239 usec per loop
rosuav@sikorsky:~$ python3 -m timeit -s "x = 2.357e7" -s "from math
import sqrt" "sqrt(x)"
10000000 loops, best of 3: 0.102 usec per loop
rosuav@sikorsky:~$ python3 -m timeit -s "x = 2.357e7" -s "import math"
"math.sqrt(x)"
10000000 loops, best of 3: 0.155 usec per loop

ChrisA

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


Thread

How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-20 13:22 -0700
  Re: How can I make this piece of code even faster? Fabio Zadrozny <fabiofz@gmail.com> - 2013-07-20 18:05 -0300
  Re: How can I make this piece of code even faster? Roy Smith <roy@panix.com> - 2013-07-20 17:25 -0400
  Re: How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-20 15:45 -0700
  Re: How can I make this piece of code even faster? Chris Angelico <rosuav@gmail.com> - 2013-07-21 08:55 +1000
  Re: How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-20 16:24 -0700
    Re: How can I make this piece of code even faster? Chris Angelico <rosuav@gmail.com> - 2013-07-21 09:29 +1000
  Re: How can I make this piece of code even faster? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-21 05:11 +0000
    Re: How can I make this piece of code even faster? Paul Rudin <paul.nospam@rudin.co.uk> - 2013-07-21 08:11 +0100
      Re: How can I make this piece of code even faster? Chris Angelico <rosuav@gmail.com> - 2013-07-21 19:21 +1000
  Re: How can I make this piece of code even faster? Peter Otten <__peter__@web.de> - 2013-07-21 09:10 +0200
  Re: How can I make this piece of code even faster? Serhiy Storchaka <storchaka@gmail.com> - 2013-07-21 10:11 +0300
  Re: How can I make this piece of code even faster? Christian Gollwitzer <auriocus@gmx.de> - 2013-07-21 09:24 +0200
  Re: How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-21 03:19 -0700
    Re: How can I make this piece of code even faster? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-21 10:31 +0000
      Re: How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-21 03:48 -0700
        Re: How can I make this piece of code even faster? Stefan Behnel <stefan_ml@behnel.de> - 2013-07-21 14:49 +0200
      Re: How can I make this piece of code even faster? Chris Angelico <rosuav@gmail.com> - 2013-07-21 20:48 +1000
    Re: How can I make this piece of code even faster? Michael Torrie <torriem@gmail.com> - 2013-07-21 09:27 -0600
  Re: How can I make this piece of code even faster? Joshua Landau <joshua@landau.ws> - 2013-07-21 12:39 +0100

csiph-web