Generator slower than iterator?

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Tue Dec 16 17:44:42 CET 2008


MRAB:
> from collections import defaultdict
> match_counter = defaultdict(int)
> for line in fileinput.input(sys.argv[1:]):
>      ip = line.split()[0]
>      match_counter[ip] += 1

This can be a little faster still:

match_counter = defaultdict(int)
for line in fileinput.input(sys.argv[1:]):
    ip = line.split(None, 1)[0]
    match_counter[ip] += 1

Bye,
bearophile



More information about the Python-list mailing list