Last active
August 29, 2015 14:24
-
-
Save komuw/5a3dc3d7bfb0a55aa029 to your computer and use it in GitHub Desktop.
python read file from memory
This file contains 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
# We compare/measure reading a file from memory(1) vs reading from filesytem(2) | |
################################## | |
### 1. reading file from memory ## | |
################################## | |
import time | |
import mmap | |
filename = "myfile.txt" | |
start = time.clock() | |
txt = open(filename) | |
# memory-map the file, size 0 means whole file | |
# mmap.PROT_READ == open for read | |
mm = mmap.mmap(txt.fileno(), 0, prot=mmap.PROT_READ) | |
for i in range(0, 100001): | |
#read 10bytes | |
mm.read(10) | |
txt.close() | |
mm.close() | |
end = time.clock() | |
duration= (end - start) * 1000 | |
print "mm_duration::", duration | |
#results: | |
""" | |
(trusty)komu@localhost:~/work$ python read_mmap.py | |
mm_duration:: 35.923 | |
(trusty)komu@localhost:~/work$ python read_mmap.py | |
mm_duration:: 36.191 | |
(trusty)komu@localhost:~/work$ python read_mmap.py | |
mm_duration:: 36.898 | |
""" | |
###################################### | |
### 2. reading file from filesystem ## | |
###################################### | |
import time | |
filename = "myfile.txt" | |
start = time.clock() | |
txt = open(filename) | |
for i in range(0, 100001): | |
#read 10bytes | |
txt.read(10) | |
txt.close() | |
end = time.clock() | |
duration= (end - start) * 1000 | |
print "duration::", duration | |
#results: | |
""" | |
(trusty)komu@localhost:~/work$ python read.py | |
duration:: 110.113 | |
(trusty)komu@localhost:~/work$ python read.py | |
duration:: 94.076 | |
(trusty)komu@localhost:~/work$ python read.py | |
duration:: 94.838 | |
# Conclusion: | |
""" | |
this seems to imply reading from memory is about 3times faster than from filesystem | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment