Skip to content

Commit 74c7101

Browse files
committed
bench: Three ways to process a byte buffer.
1 parent 59ced65 commit 74c7101

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

tests/bench/bytebuf-1-inplace.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Doing some operation on bytearray
2+
# Inplace - the most memory efficient way
3+
import bench
4+
5+
def test(num):
6+
for i in iter(range(num//10000)):
7+
ba = bytearray(b"\0" * 1000)
8+
for i in range(len(ba)):
9+
ba[i] += 1
10+
11+
bench.run(test)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Doing some operation on bytearray
2+
# Pretty weird way - map bytearray thru function, but make sure that
3+
# function return bytes of size 1, then join them together. Surely,
4+
# this is slowest way to do it.
5+
import bench
6+
7+
def test(num):
8+
for i in iter(range(num//10000)):
9+
ba = bytearray(b"\0" * 1000)
10+
ba2 = b''.join(map(lambda x:bytes([x + 1]), ba))
11+
12+
bench.run(test)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Doing some operation on bytearray
2+
# No joins, but still map().
3+
import bench
4+
5+
def test(num):
6+
for i in iter(range(num//10000)):
7+
ba = bytearray(b"\0" * 1000)
8+
ba2 = bytearray(map(lambda x: x + 1, ba))
9+
10+
bench.run(test)

0 commit comments

Comments
 (0)