A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2002-April/023735.html below:

[Python-Dev] Use for enumerate()

[Python-Dev] Use for enumerate()Tim Peters tim.one@comcast.net
Sun, 28 Apr 2002 00:36:52 -0400
>>>>> Challenge 3: do it faster and with less code.
>>> it should work even if the file is too large
>>> to fit in memory (as long as each individual line fits in memory).

[Raymond Hettinger]
> def getline(filename, lineno):
>     if lineno < 1:
>         return ''
>     f = open(filename)
>     g = f.readline
>     return (reduce(lambda x,y: g(), range(lineno), ''), f.close())[0]

This is shorter than Guido's, but you don't imagine it's faster, right?
Calling a Python-level function for each line read is more expensive than a
few measly trips around the eval loop for each line read, even if the latter
has to build a two-tuple each time.  Guido's comes in at about 4.8 seconds
on my test, and this version at about 5.5.  I believe my first stab is still
the quickest correct version we've seen, at about 2.9.





RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4