nearest neighbor in 2D

Peter Otten __peter__ at web.de
Wed Nov 5 09:40:23 CET 2003


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 :-(

> 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.

Peter






More information about the Python-list mailing list