Skip to content

Instantly share code, notes, and snippets.

@Flid
Created August 22, 2018 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Flid/1e4862beb00a63b01955b8c195502174 to your computer and use it in GitHub Desktop.
Save Flid/1e4862beb00a63b01955b8c195502174 to your computer and use it in GitHub Desktop.
block vs single-byte write
from time import perf_counter
from contextlib import contextmanager
N = 1000000
BLOCK_SIZE = 10000
@contextmanager
def measure_time(name):
start_time = perf_counter()
yield
print(f'Execution for "{name}" took {perf_counter()-start_time}')
with open('/tmp/test', 'wb') as fd:
with measure_time('single'):
for _ in range(N):
fd.write(b'0')
fd.flush()
with measure_time('block'):
for _ in range(N // BLOCK_SIZE):
fd.write(b'0' * BLOCK_SIZE)
fd.flush()
# Execution for "single" took 0.17417324799998823
# Execution for "block" took 0.0005379629997150914
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment