Skip to content

Instantly share code, notes, and snippets.

@davidwtbuxton
Last active December 7, 2017 19:50
Show Gist options
  • Save davidwtbuxton/ef752a7bbf233386adf8c11b5a8dd7f7 to your computer and use it in GitHub Desktop.
Save davidwtbuxton/ef752a7bbf233386adf8c11b5a8dd7f7 to your computer and use it in GitHub Desktop.
Seeing how reading an image file from disk compares to constructing it in memory
import io
import os
import tempfile
import timeit
# A valid PNG file.
png_bytes = (
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08'
'\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\nIDATx\x9cc\x00\x01\x00\x00'
'\x05\x00\x01\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82'
)
png_len = len(png_bytes)
# Create the image on disk.
_, filename = tempfile.mkstemp(suffix='.png')
with open(filename, 'wb') as fh:
fh.write(png_bytes)
def read_from_disk(filename):
with open(filename) as fh:
data = fh.read()
assert len(data) == png_len
def read_from_memory(filename):
fh = io.BytesIO(png_bytes)
data = fh.read()
assert len(data) == png_len
setup = 'from __main__ import read_from_disk, read_from_memory, filename'
statements = ['read_from_memory(filename)', 'read_from_disk(filename)']
for s in statements:
timer = timeit.Timer(s, setup=setup)
print s, timer.repeat()
os.unlink(filename)
$ python2.7 memory_vs_disk.py
read_from_memory(filename) [1.0891728401184082, 1.0940730571746826, 1.105133056640625]
read_from_disk(filename) [11.179327011108398, 10.985939025878906, 11.328207969665527]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment