[Python-Dev] Use for enumerate() (original) (raw)
Tim Peters tim.one@comcast.net
Sun, 28 Apr 2002 00:36:52 -0400
- Previous message: Fw: [Python-Dev] Use for enumerate()
- Next message: [Python-Dev] Re: Use for enumerate
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Previous message: Fw: [Python-Dev] Use for enumerate()
- Next message: [Python-Dev] Re: Use for enumerate
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]