Created
August 22, 2018 10:04
-
-
Save Flid/1e4862beb00a63b01955b8c195502174 to your computer and use it in GitHub Desktop.
block vs single-byte write
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
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