Skip to content

Instantly share code, notes, and snippets.

@toruw
Last active August 29, 2015 14:17
Show Gist options
  • Save toruw/f80b1dc8a712bab03c27 to your computer and use it in GitHub Desktop.
Save toruw/f80b1dc8a712bab03c27 to your computer and use it in GitHub Desktop.
IO Performance comparison
import mmap,time
def test_io():
f = open('test.dat','r+b',0)
start = time.time()
i = 0
while i < 1000000:
f.read()
i += 1
end = time.time()
print("test_io: %s ms" % ((end - start)*1000))
def test_io_all():
f = open('test.dat','r+b',0)
start = time.time()
f.read(1000000)
end = time.time()
print("test_io_all: %s ms" % ((end - start)*1000))
def test_mmap():
f = open('test.dat','r+b')
m = mmap.mmap(f.fileno(),0)
start = time.time()
i = 0
while i < 1000000:
m.read_byte()
i += 1
end = time.time()
print("test_mmap: %s ms" % ((end - start)*1000))
def test_mmap_all():
f = open('test.dat','r+b')
m = mmap.mmap(f.fileno(),0)
start = time.time()
m.read(1000000)
end = time.time()
print("test_mmap_all: %s ms" % ((end - start)*1000))
if __name__ == '__main__':
test_io()
test_io_all()
test_mmap()
test_mmap_all()
@toruw
Copy link
Author

toruw commented Mar 29, 2015

Result

test_io: 1734.96699333 ms
test_io_all: 0.364065170288 ms
test_mmap: 186.017990112 ms
test_mmap_all: 0.677824020386 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment