[SciPy-user] Performance question.

Duncan Child duncan at enthought.com
Wed Mar 2 17:18:27 CST 2005


I am working on developing algorithms that are usually called with 
parameters that are Numeric arrays.  We have the usual challenge though 
of trying to craft code that will gracefully accept both floats or 
arrays. Because of the often discussed problem with the handling of zero 
length arrays we expend some effort to ensure that we don't make calls 
on floats that only work on arrays and have ended up with a bunch of 
code like safe_len() that can be called with either.

Today we have a related problem but now it is with performance (see code 
below). Numeric is faster than NumArray operating on smaller arrays but 
it is still relatively slow handling regular floats. We could add to the 
safe_ suite of functions the fast_ series but this still entails a 
significant performance hit and is not exactly elegant.

The problem is larger than just handling sqrt so I would appreciate any 
feedback or suggestions on how best to proceed.

Thanks,

Duncan

=================================================
def safe_len(a):
    # Return the length of the input array or 1 if it is a scalar
    try:
        safelen = len(a)
    except:
        safelen = 1
    return safelen
=================================================
from scipy import arange, sqrt
from math import sqrt as csqrt
import time

# this is slower ...
start_time = time.clock()
for i in range(1000):
    a = sqrt(i)
t1 = time.clock() - start_time   
  
  
# this is faster ...
start_time = time.clock()
for i in range(1000):
    a = csqrt(i)
t2 = time.clock() - start_time   
   
print t1, t2, t1 / t2

C:\sqrt.py
0.0537227007132 0.00181048033684 68.6731754663
=================================================
from scipy import sqrt
from math import sqrt as csqrt
import types

def fast_sqrt(arg):
    if type(arg) == types.FloatType:
        return csqrt(arg)
    else:
        return sqrt(arg)







More information about the SciPy-user mailing list