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


Groups > comp.lang.python > #6978

Re: Best way to compute length of arbitrary dimension vector?

References (1 earlier) <mailman.2258.1306749013.9059.python-list@python.org> <a951396d-446b-4ad6-8ad3-d12420e251af@hg8g2000vbb.googlegroups.com> <201106030726.21147.akabaila@pcug.org.au> <mailman.2409.1307056774.9059.python-list@python.org> <c48d6e64-696a-4d4f-9099-c9137bf25fac@16g2000yqy.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-06-03 16:17 -0600
Subject Re: Best way to compute length of arbitrary dimension vector?
Newsgroups comp.lang.python
Message-ID <mailman.2441.1307139504.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jun 3, 2011 at 3:53 PM, Gabriel <snoopy.67.z@googlemail.com> 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

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


Thread

Best way to compute length of arbitrary dimension vector? Gabriel <snoopy.67.z@googlemail.com> - 2011-05-30 02:11 -0700
  Re: Best way to compute length of arbitrary dimension vector? Chris Rebert <clp2@rebertia.com> - 2011-05-30 02:24 -0700
  Re: Best way to compute length of arbitrary dimension vector? Peter Otten <__peter__@web.de> - 2011-05-30 11:46 +0200
    Re: Best way to compute length of arbitrary dimension vector? Gabriel <snoopy.67.z@googlemail.com> - 2011-05-30 06:38 -0700
      Re: Best way to compute length of arbitrary dimension vector? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-02 17:19 -0600
        Re: Best way to compute length of arbitrary dimension vector? Gabriel <snoopy.67.z@googlemail.com> - 2011-06-03 14:53 -0700
          Re: Best way to compute length of arbitrary dimension vector? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-03 16:17 -0600
          Re: Best way to compute length of arbitrary dimension vector? Robert Kern <robert.kern@gmail.com> - 2011-06-03 18:12 -0500
  Re: Best way to compute length of arbitrary dimension vector? "Gabriel Genellina" <gagsl-py2@yahoo.com.ar> - 2011-05-30 16:01 -0300
    Re: Best way to compute length of arbitrary dimension vector? Gabriel <snoopy.67.z@googlemail.com> - 2011-06-01 12:35 -0700

csiph-web