Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

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
0%
Account Age
14 years
Verified Email
Yes
Verified Flair
No
Total Karma
655,951
Link Karma
27,441
Comment Karma
616,964
Profile updated: 3 months ago
Posts updated: 3 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