Generator speed in python 3

Posted by Will on Stack Overflow See other posts from Stack Overflow or by Will
Published on 2010-03-08T04:35:22Z Indexed on 2010/03/08 4:36 UTC
Read the original article Hit count: 251

Filed under:

Hello all,

I am going through a link about generators that someone posted. In the beginning he compares the two functions below. On his setup he showed a speed increase of 5% with the generator.

I'm running windows XP, python 3.1.1, and cannot seem to duplicate the results. I keep showing the "old way"(logs1) as being slightly faster when tested with the provided logs and up to 1GB of duplicated data.

Can someone help me understand whats happening differently?

Thanks!

def logs1():
wwwlog = open("big-access-log")
total = 0
for line in wwwlog:
    bytestr = line.rsplit(None,1)[1]
    if bytestr != '-':
        total += int(bytestr)
return total

def logs2():
wwwlog = open("big-access-log")
bytecolumn = (line.rsplit(None,1)[1] for line in wwwlog)
getbytes      = (int(x) for x in bytecolumn if x != '-')
return sum(getbytes)

© Stack Overflow or respective owner

Related posts about python