Last active
December 15, 2015 08:09
-
-
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.
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
on 2.7 GHz Intel Core i7, 8G RAM, SSD disk.