nearest neighbor in 2D
Jim Richardson
warlock at eskimo.com
Wed Nov 5 21:26:49 EST 2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Wed, 05 Nov 2003 09:40:23 +0100,
Peter Otten <__peter__ at web.de> wrote:
> Jim Richardson wrote:
>
>> I am new to timeit.py, but this is odd.
>>
>> jim at grendel:~$ /usr/lib/python2.3/timeit.py -c ' p=1.6+2.5j;np=2.4+1.3j;
>> d=abs(p-np)' 100000 loops, best of 3: 3.1 usec per loop
>
> The above is actually timed.
>
>> jim at grendel:~$ /usr/lib/python2.3/timeit.py -c -s'import math;
>> p=1.6+2.5j;np=2.4+1.3j; d=abs(p-np)' 10000000 loops, best of 3: 0.141 usec
>> per loop
>
> Here you put everything in the setup parameter. As the timed statement
> defaults to pass, you are essentially timing an empty loop :-(
>
Ah! that explains it, thanks. breaking the import off, gets the loop to
3.2 usec per loop, which makes more sense.
>> Is it because the builtin math functions are much slower?
>
> You should have used math.abs() or from math import abs to really get the
> abs() function in the math module. But
>
>>>> "abs" in dir(math)
> False
>
> Not there, so we cannot compare.
Actually, I just cut'n'pasted, with a tiny alteration (timeit.py isn't
in my path) I like the idea of using the complex number there, it's a
tad easier that the other method for me to visualize.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQE/qbFpd90bcYOAWPYRArgzAKDiNBXw3tIpooWqglmxoqn2GebyxgCcDEKV
u60SPPopUrkJ1Kak0t4eias=
=+Guh
-----END PGP SIGNATURE-----
--
Jim Richardson http://www.eskimo.com/~warlock
"Man invented language to satisfy his deep need to complain."
-- Lily Tomlin
More information about the Python-list
mailing list