Sequence traversal

Darrell news at dorb.com
Thu Sep 23 13:57:06 EDT 1999


I need to start using map more often. This was a neat use. Here's some
thoughts on speed.

import time

sz=30000
l1=range(0,sz)
l2=range(sz,2*sz)

searchKey=(sz-1,2*sz-1)

def test1():
    cnt=0
    if searchKey in map(None,l1,l2):
        cnt=cnt+1
    return cnt

class Test2:
    def __init__(self):
        self._cache={}
            # I've never gotten used to lamda
        def func(a,b,c=self._cache):
            c[(a,b)]=1
        self._cacheFunc=func

    def __call__(self):
        cnt=0
        if len(self._cache)==0:
            map(self._cacheFunc,l1,l2)
        if self._cache.get(searchKey,None):
            cnt=cnt+1
        return cnt

test2=Test2()


def tester(func, maxLoop=1):
    t1=time.time()
    cnt=0
    print 'maxLoop:',maxLoop
    for x in range(maxLoop):
        cnt=func()
    print 'Found:',cnt
    t2=time.time()
    print 'Time: %.4f'%(t2-t1)
    print


tester(test1)
tester(test2)
print '*'*10
tester(test1,100)
tester(test2,100)

"""
maxLoop: 1
Found: 1
Time: 0.0940

maxLoop: 1
Found: 1
Time: 0.1720

**********
maxLoop: 100
Found: 1
Time: 5.6560

maxLoop: 100
Found: 1
Time: 0.0310
"""







--
--Darrell






More information about the Python-list mailing list