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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Sun, Jul 21, 2013 at 8:31 PM, Steven D'Aprano 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