Created
May 26, 2015 15:07
-
-
Save asauber/22e930fe8a2196147f18 to your computer and use it in GitHub Desktop.
Fast way to read integers from file in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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