Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.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.046 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'pm,': 0.10; 'wrote:': 0.14; '"from': 0.16; '0)"': 0.16; 'functools': 0.16; 'really.': 0.16; 'math': 0.16; 'cc:addr:python-list': 0.17; 'header:In-Reply- To:1': 0.21; 'loop': 0.22; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'fri,': 0.23; 'received:209.85.161.46': 0.23; 'received:mail-fx0-f46.google.com': 0.23; 'received:209.85.161': 0.26; 'message-id:@mail.gmail.com': 0.28; 'subject:?': 0.29; 'import': 0.29; 'cc:addr:python.org': 0.30; 'expression': 0.32; 'list': 0.33; 'skip:" 10': 0.35; 'using': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'but': 0.38; 'subject:: ': 0.38; 'received:209': 0.39; 'really': 0.40; 'best': 0.60; '10000': 0.91; '100000': 0.91; 'gabriel': 0.91; 'subject:Best': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=apC1t49rkbT9dqQkxcRKeuZVCN/q9uZiZmLwEphfXP4=; b=eCESEe3EiCDTPrauuQgM6n31xCcuVMqfL8Go9U1+ZaD/PozuS4UQ6UL8131l08sG/u KaOqTBMSDHlzRcV4Rp496HrAoF3GO/xKw1B4LCdcfVV/BoD6LMKFOm6LQivmf/A2Igtl ou066ACOn80l0fvUmqvUDsSS02u/Uq2xtjxaY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=MfCF1vi98g8XF+/JzB5dagWPHBk3rEYVQww1QD791DI09RxjiKGiBm9W1HSY0wK867 nLQLWkpiuugCuCNC203HLOBxS0/RNZZHaTOU3ObGMqvACmTeBKt4FpwRvz+2PYJSY3ba hf5hnuhfI6D5O93RHe0E84rdBKtDT00OmWLN8= MIME-Version: 1.0 In-Reply-To: References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> <201106030726.21147.akabaila@pcug.org.au> From: Ian Kelly Date: Fri, 3 Jun 2011 16:17:53 -0600 Subject: Re: Best way to compute length of arbitrary dimension vector? To: Gabriel Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 14 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307139504 news.xs4all.nl 49184 [::ffff:82.94.164.166]:36180 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6978 On Fri, Jun 3, 2011 at 3:53 PM, Gabriel wrote: > But still, is this solution really faster or better than the one using > list comprehension and the expression 'x*x'? No, not really. >c:\python32\python -m timeit -s "coords = list(range(100))" -s "from math import hypot" -s "from functools import reduce" "reduce(hypot, coords, 0)" 10000 loops, best of 3: 53.2 usec per loop >c:\python32\python -m timeit -s "coords = list(range(100))" -s "from math import sqrt, fsum" "sqrt(fsum(x*x for x in coords))" 10000 loops, best of 3: 32 usec per loop >c:\python32\python -m timeit -s "coords = list(range(100))" -s "from math import sqrt" "sqrt(sum(x*x for x in coords))" 100000 loops, best of 3: 14.4 usec per loop