Skip to content

Instantly share code, notes, and snippets.

@littleq0903
Created October 3, 2013 13:10
Show Gist options
  • Save littleq0903/6809569 to your computer and use it in GitHub Desktop.
Save littleq0903/6809569 to your computer and use it in GitHub Desktop.
Proof of Python's join is faster than string concatenation.
>>> a = ["a","b","c"]
>>> def A():
... return a[0]+a[1]+a[2]
...
>>> def B():
... return "".join(a)
...
>>> import dis
>>> dis.dis(A)
2 0 LOAD_GLOBAL 0 (a)
3 LOAD_CONST 1 (0)
6 BINARY_SUBSCR
7 LOAD_GLOBAL 0 (a)
10 LOAD_CONST 2 (1)
13 BINARY_SUBSCR
14 BINARY_ADD
15 LOAD_GLOBAL 0 (a)
18 LOAD_CONST 3 (2)
21 BINARY_SUBSCR
22 BINARY_ADD
23 RETURN_VALUE
>>> dis.dis(B)
2 0 LOAD_CONST 1 ('')
3 LOAD_ATTR 0 (join)
6 LOAD_GLOBAL 1 (a)
9 CALL_FUNCTION 1
12 RETURN_VALUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment