Skip to content

Instantly share code, notes, and snippets.

@komuw
Last active August 29, 2015 14:24
Show Gist options
  • Save komuw/5a3dc3d7bfb0a55aa029 to your computer and use it in GitHub Desktop.
Save komuw/5a3dc3d7bfb0a55aa029 to your computer and use it in GitHub Desktop.
python read file from memory
# 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