2
[Python] Reading in a list of integers: the text to integer conversion seems really slow
Author Summary
Astrokiwi is
in
PYTHON
Post Body
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
Author
Account Strength
100%
Account Age
14 years
Verified Email
Yes
Verified Flair
No
Total Karma
662,790
Link Karma
27,508
Comment Karma
623,736
Profile updated: 4 days ago
Posts updated: 4 months ago
Subreddit
Post Details
Location
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 10 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...