why scipy cause my program slow?

HYRY zhangry at feng.co.jp
Tue Jan 16 03:12:04 EST 2007


Why the exec time of test(readdata()) and test(randomdata()) of
following program is different?
my test file 150Hz10dB.wav has 2586024 samples, so I set randomdata
function
to return a list with 2586024 samples.
the exec result is:
2586024
<type 'list'>
10.8603842736
2586024
<type 'list'>
2.16525233979
test(randomdata()) is 5x faster than test(readdata())
if I remove "from scipy import *" then I get the following result:
2586024
<type 'list'>
2.21851601473
2586024
<type 'list'>
2.13885042216

So, what the problem with scipy?
Python 2.4.2, scipy ver. 0.5.1


import wave
from scipy import *
from time import *
import random
from array import array

def readdata():
    f = wave.open("150Hz10dB.wav", "rb")
    t = f.getparams()
    SampleRate = t[2]
    data = array("h", f.readframes(t[3]))
    f.close()
    left = data[0::2]
    mean = sum(left)/float(len(left))
    left = [abs(x-mean) for x in left]
    return left

def randomdata():
    return  [random.random()*32768.0 for i in xrange(2586024)]

def test(data):
    print len(data)
    print type(data)
    envelop = []
    e = 0.0
    ga, gr = 0.977579425259, 0.999773268338
    ga1, gr1 = 1.0 - ga, 1.0 - gr
    start = clock()
    for x in data:
        if e < x:
            e *= ga
            e += ga1*x
        else:
            e *= gr
            e += gr1*x
        envelop.append(e)
    print clock() - start
    return envelop

test(readdata())    
test(randomdata())




More information about the Python-list mailing list