Skip to content

Instantly share code, notes, and snippets.

@msalvadores
Last active December 15, 2015 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msalvadores/5229030 to your computer and use it in GitHub Desktop.
Save msalvadores/5229030 to your computer and use it in GitHub Desktop.
A benchmark of two approaches to constructing an array. Bytearray vs array+join. Limitation: one should know the length of the array. Within pypy-1.9 the bytearray approach is one order of magnitude faster. In CPython-2.7, bytearray aprox. 35% faster.
import timeit
DATA = 'abcdef'
def bytearr():
bytesn = 1024 * 1024 * 5
s = bytearray(bytesn)
i = 0
while i < bytesn:
s[i] = DATA[i%len(DATA)]
i += 1
return str(s)
def strs():
bytesn = 1024 * 1024 * 5
s = []
i = 0
while i < bytesn:
s.append(DATA[i%len(DATA)])
i += 1
ss = "".join(s)
return ss
if __name__ == "__main__":
print timeit.timeit(bytearr,number=10)
print timeit.timeit(strs,number=10)
#(pypy-1.9)➜ ✗ python array_append_len_known.py
#0.774618959427
#9.52767682076
#(cPyhton-2.7)➜ ✗ python array_append_len_known.py
#16.6827712059
#21.0312628746
@msalvadores
Copy link
Author

on 2.7 GHz Intel Core i7, 8G RAM, SSD disk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment