Generator slower than iterator?

Federico Moreira federico at linux.com.uy
Tue Dec 16 10:07:14 EST 2008


Hi all,

Im parsing a 4.1GB apache log to have stats about how many times an ip
request something from the server.

The first design of the algorithm was

for line in fileinput.input(sys.argv[1:]):
    ip = line.split()[0]
    if match_counter.has_key(ip):
        match_counter[ip] += 1
    else:
        match_counter[ip] = 1

And it took 3min 58 seg to give me the stats

Then i tried a generator solution like

def generateit():
    for line in fileinput.input(sys.argv[1:]):
        yield line.split()[0]

for ip in generateit():
    ...the same if sentence

Instead of being faster it took 4 min 20 seg

Should i leave fileinput behind?
Am i using generators with the wrong aproach?

Thanks in advance,

Federico.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081216/2e33f364/attachment-0001.html>


More information about the Python-list mailing list