This post has been de-listed (Author was flagged for spam)
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
So I'm just wanting to do:
f = open(infile)
md = max([int(x) for x in f.readlines()[1:]])
f.close()
but it seems to be running much slower than I was expecting. If I split this up into steps and time each one, thusly:
f = open(infile)
start=time.clock()
data = f.readlines()
print time.clock()-start
start=time.clock()
idata = [int(x) for x in data[1:]]
print time.clock()-start
start=time.clock()
md = max(idata)
print time.clock()-start
f.close()
I find that the conversion of text to integers takes 4 times as long as reading in the data from the disk! Specifically, for a 1.4 million entry array, it takes ~0.2-0.3 seconds to read in the data, and 0.9-1 seconds to do the conversion, and ~0.06 seconds to find the maximum value.
Are there any tricks to more directly or efficiently read in a list of integers in Python? I have ~2500 files to read in, and at >1 second each that's coming up close to an hour, which is fine, but it seems like it shouldn't need to take that long. Unless there's a neat Python trick, I might have to resort to Fortran :P
Subreddit
Post Details
- Posted
- 10 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...