Skip to content

Instantly share code, notes, and snippets.

@asauber
Created May 26, 2015 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asauber/22e930fe8a2196147f18 to your computer and use it in GitHub Desktop.
Save asauber/22e930fe8a2196147f18 to your computer and use it in GitHub Desktop.
Fast way to read integers from file in Python
from datetime import datetime
import fileinput
start = datetime.now()
L = []
for line in fileinput.input():
L.append(int(line))
print 'time to read 8000000 ints into list using append: ', datetime.now() - start
print L
from datetime import datetime
import fileinput
start = datetime.now()
A = map(int, [line for line in fileinput.input()])
print 'time to read 8000000 ints into list using generator and map: ', datetime.now() - start
print A
time to read 8000000 ints into list using append: 0:00:10.719517
time to read 8000000 ints into list using append: 0:00:10.782932
time to read 8000000 ints into list using append: 0:00:10.777267
time to read 8000000 ints into list using generator and map: 0:00:09.966590
time to read 8000000 ints into list using generator and map: 0:00:09.888658
time to read 8000000 ints into list using generator and map: 0:00:09.799702
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment