Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Created June 28, 2012 05:46
Show Gist options
  • Save kurtbrose/3009357 to your computer and use it in GitHub Desktop.
Save kurtbrose/3009357 to your computer and use it in GitHub Desktop.
experimenting with bytearray
maybe a way to squeeze a bit more out of these short string appends
>>> def list_method():
... l = list()
... for i in range(10000):
... l.append(b'\x01')
... return "".join(l)
...
>>> a = list_method()
>>> def byte_array_method():
... b = bytearray(10000)
... for i in range(10000):
... b[i] = 1
... return b
...
>>> b = byte_array_method()
>>>
>>> timeit.timeit('b = bytearray(10000)\nfor i in range(10000): b[i] = 1', numbe
r=100)
0.14167855132424734
>>> timeit.timeit('l=[]\nfor i in range(10000): l.append(b"\x01")\n"".join(l)',
number=100)
0.18589855693949175
>>> 0.185898/0.14167855
1.3121111135030674
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment