Generator slower than iterator?
MRAB
google at mrabarnett.plus.com
Fri Dec 19 11:16:12 EST 2008
Federico Moreira wrote:
> Great, 2min 34 secs with the open method =)
>
> but why?
>
> ip, sep, rest = line.partition(' ')
> match_counter[ip] += 1
>
> instead of
>
> match_counter[line.strip()[0]] += 1
>
> strip really takes more time than partition?
>
> I'm having the same results with both of them right now.
>
I think you meant split(), not strip().
split() might split the string into many parts, but you want only the
first part (at most 1 split), so the extra splits are unnecessary and
waste time. split(None, 1) and partition(' ') do at most 1 split, so
they don't do unnecessary work.
More information about the Python-list
mailing list