Strange Execution Times

curi42 at gmail.com curi42 at gmail.com
Thu May 26 02:14:19 EDT 2005


I am running two functions in a row that do the same thing.  One runs
in .14 seconds, the other 56.  I'm confused.  I wrote another version
of the program and couldn't get the slow behavior again, only the fast.
 I'm not sure what is causing it.  Can anyone figure it out?

Here is my code (sorry it's a bit of a mess, but my cleaned up version
isn't slow!).  Just skim to the bottom where the timing is.  The first
time printed out is .14, the seond is 56.56.


f = open("/Users/curi/data.xml")


o = open("/Users/curi/out2.xml", "w")


import md5
import array


p1 = "<Password>"
p2 = "</Password>"

cnt = 0

m = md5.new
jo = "".join


adjust = len(p1) - 1

i = 1
s = f.read()
a = array.array('c', s).tolist()
spot = 0
k = 0
find = s.find

starts = []
ends = []

while k != -1:

    #print len(s)
    k = find(p2, spot)
    if k != -1:
        starts.append(find(p1, spot) + adjust)
        ends.append(k)
        spot = k + 1

    #s = "".join([s[:j+1], md5.new(s[j+1:k-1]).hexdigest(), s[k:]])

    #if k != -1: a[j+1:k-1] = m(jo(a[j+1:k-1])).hexdigest()



r = range(len(starts))
#r = range(20)
r.reverse()
import time


data = a[:]

md5 = m
join = jo





t1 = time.clock()
for j in r:
    #print jo(s[starts[j]+1:ends[j]])
    digest = m(jo(s[starts[j]+1:ends[j]])).hexdigest()

    a[starts[j]+1:ends[j]] = digest
    #cnt += 1
    #if cnt % 100 == 0: print cnt


t2 = time.clock()
print "time is", round(t2-t1, 5)



t1 = time.clock()
for i in r:
    data[starts[i]:ends[i]] =
md5(join(s[starts[i]:ends[i]])).hexdigest()
t2 = time.clock()
print "second time is", round(t2-t1, 5)


o.write(jo(a))




More information about the Python-list mailing list