Skip to content

Instantly share code, notes, and snippets.

@generalmimon
Last active May 22, 2024 08:35
Show Gist options
  • Save generalmimon/dcaec4d005d0d1a87bd237d94199203a to your computer and use it in GitHub Desktop.
Save generalmimon/dcaec4d005d0d1a87bd237d94199203a to your computer and use it in GitHub Desktop.
bytes_terminate() benchmark for the Kaitai Struct Python runtime library: https://github.com/kaitai-io/kaitai_struct_python_runtime/blob/d457617/kaitaistruct.py#L447
"""
Usage:
$ richbench --markdown .
You need to install https://pypi.org/project/richbench/ first:
$ python -m pip install -U richbench
"""
import random
TERM_HALFWAY = 1
TERM_END = 2
TERM_MISSING = 3
# Settings
TERM_POS = TERM_MISSING
INCLUDE_TERM = False
WORKLOAD_SIZE = 128 * 1024
NUM_ITERS = 20000
def bytes_terminate(data, term, include_term):
new_data, term_byte, _ = data.partition(bytes((term,)))
if include_term:
new_data += term_byte
return new_data
def bytes_terminate_new(data, term, include_term):
term_index = data.find(term) # KaitaiStream.byte_array_index_of(data, term)
if term_index == -1:
return data[:]
return data[:term_index + (1 if include_term else 0)]
term = random.randrange(256)
repl = 0x00
if repl == term:
repl = 0xff
buf = bytearray(random.randbytes(WORKLOAD_SIZE))
for i, b in enumerate(buf):
if b == term:
buf[i] = repl
if TERM_POS == TERM_HALFWAY:
buf[len(buf) // 2] = term
elif TERM_POS == TERM_END:
buf[-2] = term
elif TERM_POS == TERM_MISSING:
pass
buf = bytes(buf)
if TERM_POS == TERM_HALFWAY:
exp = len(buf) // 2 + (1 if INCLUDE_TERM else 0)
elif TERM_POS == TERM_END:
exp = len(buf) - 2 + (1 if INCLUDE_TERM else 0)
elif TERM_POS == TERM_MISSING:
exp = len(buf)
act = len(bytes_terminate(buf, term, INCLUDE_TERM))
assert act == exp, 'expected {} but got {}'.format(exp, act)
act = len(bytes_terminate_new(buf, term, INCLUDE_TERM))
assert act == exp, 'expected {} but got {}'.format(exp, act)
def old():
for _ in range(NUM_ITERS):
bytes_terminate(buf, term, INCLUDE_TERM)
def new():
for _ in range(NUM_ITERS):
bytes_terminate_new(buf, term, INCLUDE_TERM)
__benchmarks__ = [
(old, new, "old vs new"),
]
'''
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ python3 --version
Python 3.10.12
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ sed -n '19,22p;23q' bench_bytes_terminate.py
TERM_POS = TERM_HALFWAY
INCLUDE_TERM = False
WORKLOAD_SIZE = 128 * 1024
NUM_ITERS = 20000
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ richbench --markdown --repeat 10 .
Benchmarks, repeat=10, number=5
| Benchmark | Min | Max | Mean | Min (+) | Max (+) | Mean (+) |
|------------|---------|---------|---------|-----------------|-----------------|-----------------|
| old vs new | 0.376 | 0.385 | 0.379 | 0.204 (1.85x) | 0.212 (1.82x) | 0.208 (1.82x) |
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ sed -n '19,22p;23q' bench_bytes_terminate.py
TERM_POS = TERM_HALFWAY
INCLUDE_TERM = True
WORKLOAD_SIZE = 128 * 1024
NUM_ITERS = 20000
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ richbench --markdown --repeat 10 .
Benchmarks, repeat=10, number=5
| Benchmark | Min | Max | Mean | Min (+) | Max (+) | Mean (+) |
|------------|---------|---------|---------|-----------------|-----------------|-----------------|
| old vs new | 0.560 | 0.569 | 0.564 | 0.208 (2.69x) | 0.214 (2.65x) | 0.210 (2.68x) |
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ sed -n '19,22p;23q' bench_bytes_terminate.py
TERM_POS = TERM_END
INCLUDE_TERM = False
WORKLOAD_SIZE = 128 * 1024
NUM_ITERS = 20000
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ richbench --markdown --repeat 10 .
Benchmarks, repeat=10, number=5
| Benchmark | Min | Max | Mean | Min (+) | Max (+) | Mean (+) |
|------------|---------|---------|---------|-----------------|-----------------|-----------------|
| old vs new | 0.445 | 0.459 | 0.451 | 0.450 (-1.01x) | 0.459 (-1.00x) | 0.454 (-1.01x) |
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ sed -n '19,22p;23q' bench_bytes_terminate.py
TERM_POS = TERM_END
INCLUDE_TERM = True
WORKLOAD_SIZE = 128 * 1024
NUM_ITERS = 20000
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ richbench --markdown --repeat 10 .
Benchmarks, repeat=10, number=5
| Benchmark | Min | Max | Mean | Min (+) | Max (+) | Mean (+) |
|------------|---------|---------|---------|-----------------|-----------------|-----------------|
| old vs new | 0.928 | 0.944 | 0.937 | 0.460 (2.02x) | 0.466 (2.02x) | 0.462 (2.03x) |
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ sed -n '19,22p;23q' bench_bytes_terminate.py
TERM_POS = TERM_MISSING
INCLUDE_TERM = False
WORKLOAD_SIZE = 128 * 1024
NUM_ITERS = 20000
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ richbench --markdown --repeat 10 .
Benchmarks, repeat=10, number=5
| Benchmark | Min | Max | Mean | Min (+) | Max (+) | Mean (+) |
|------------|---------|---------|---------|-----------------|-----------------|-----------------|
| old vs new | 0.145 | 0.149 | 0.147 | 0.143 (1.01x) | 0.147 (1.01x) | 0.144 (1.02x) |
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ sed -n '19,22p;23q' bench_bytes_terminate.py
TERM_POS = TERM_MISSING
INCLUDE_TERM = True
WORKLOAD_SIZE = 128 * 1024
NUM_ITERS = 20000
pp@DESKTOP-89OPGF3:/mnt/c/temp/kaitai_struct/runtime/python$ richbench --markdown --repeat 10 .
Benchmarks, repeat=10, number=5
| Benchmark | Min | Max | Mean | Min (+) | Max (+) | Mean (+) |
|------------|---------|---------|---------|-----------------|-----------------|-----------------|
| old vs new | 0.149 | 0.154 | 0.151 | 0.144 (1.04x) | 0.153 (1.01x) | 0.147 (1.03x) |
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment