quick question

John Hunter jdhunter at ace.bsd.uchicago.edu
Tue Nov 19 03:31:31 EST 2002


>>>>> "Cousin" == Cousin Stanley <CousinStanley at HotMail.com> writes:

    Cousin> John ...  I tried your suggestions of using the function

    Cousin> Did I implement your suggestions correctly ???  For
    Cousin> convenience in adding the mapped test to what I already
    Cousin> had, I used a global dictionary defintion ...

    Cousin> Would that have any significant affects on performace ???

Your implementation was fine.  The answer (as your tests show) is that
the overhead imposed by the function call is more than that of the
loop.  So my guess was wrong, which is why they say test test test
rather than talk talk talk.

I did one thing to try and improve the speed, but it didn't help much.
global lookup of the global dictionary is slow in the counting
function, so I derived a new class from dict called counting_dict

    class counting_dict(dict):
       def __call__(self, key):
          key = key.strip()	
          self[ key ] = self.get( key , 0 ) + 1 

       def get_total(self):
          return reduce(int.__add__, self.values())

which you can use like  

    votes = file('votes.dat').xreadlines()
    favorites = counting_dict()
    map(favorites, votes)
    total = favorites.get_total()   


It's nice, clean code, but it's also considerably slower than the
original, un-psyco-d, loop version


    Elapsed Time :    0.87 Seconds ... loop        (no psyco) 
    Elapsed Time :    1.31 Seconds ... custom dict (no psyco) 

    Cousin> I wasn't sure whether or not the Psyco binding was
    Cousin> necessary for the third pass through the script when the
    Cousin> mapped function is called ???

Well, in either case the map version loses to the normal version, both
psyco'd or both not.  But I would split the test cases into separate
funcs, because once you psyco bind the function, you can't un-psyco it
for further tests (AFAIK)

    Cousin>  Since the printed output lines come out double-spaced
    Cousin> in the mapped function version, where's the most
    Cousin> convenient place to stick the strip() function without
    Cousin> interfering ???

See the __call__ func above.

Note also in your code that

    else :
        if func_Type == 2 :

can be written

    elif func_Type == 2 :

Cheers,
John Hunter




More information about the Python-list mailing list