Skip to content

Instantly share code, notes, and snippets.

@poying
Created December 8, 2014 14:30
Show Gist options
  • Select an option

  • Save poying/fb589d0daffac773171b to your computer and use it in GitHub Desktop.

Select an option

Save poying/fb589d0daffac773171b to your computer and use it in GitHub Desktop.
import time
n = range(50000)
def test_tuple():
data = ()
for i in n:
data += (1, 2) + (3, 4)
def test_tuple2():
data = []
for i in n:
data += list((1, 2)) + list((3, 4))
def test_list():
data = []
for i in n:
data += [1, 2] + [3, 4]
def run(fn):
start = time.time()
fn()
print(time.time() - start)
def bench():
run(test_tuple)
run(test_tuple2)
run(test_list)
print('-' * 30)
bench()
bench()
bench()
@poying
Copy link
Author

poying commented Dec 8, 2014

python 2

13.8364539146
0.0392620563507
0.0218951702118
------------------------------
14.0117380619
0.0450830459595
0.0240149497986
------------------------------
13.4179239273
0.0443770885468
0.0231649875641
------------------------------

python 3

14.836725950241089
0.04980206489562988
0.025907039642333984
------------------------------
15.954117059707642
0.045371055603027344
0.02965688705444336
------------------------------
15.783446073532104
0.04820895195007324
0.024391889572143555
------------------------------

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