Skip to content

Instantly share code, notes, and snippets.

@IlianIliev
Created February 11, 2015 00:01
Show Gist options
  • Save IlianIliev/d3eeb7b08959909c9737 to your computer and use it in GitHub Desktop.
Save IlianIliev/d3eeb7b08959909c9737 to your computer and use it in GitHub Desktop.
bisect vs append/sort
import bisect
import timeit
import random
def test_bisect(num):
data_list = list()
for i in range(1,num):
var=random.randint(1,num*100)
loc=bisect.bisect(data_list,var)
bisect.insort(data_list,var)
def test_append_sort(num):
data_list = list()
for i in range(1,num):
var=random.randint(1,num*100)
data_list.append(var)
data_list.sort()
print timeit.timeit(setup="from __main__ import test_bisect", stmt="test_bisect(100)", number=10**4)
# 1.64354395866
print timeit.timeit(setup="from __main__ import test_append_sort", stmt="test_append_sort(100)", number=10**4)
# 1.1516520977
print timeit.timeit(setup="from __main__ import test_bisect", stmt="test_bisect(1000)", number=10**4)
# 17.6921269894
print timeit.timeit(setup="from __main__ import test_append_sort", stmt="test_append_sort(1000)", number=10**4)
# 11.5946080685
print timeit.timeit(setup="from __main__ import test_bisect", stmt="test_bisect(10000)", number=10**4)
# 292.096198082
print timeit.timeit(setup="from __main__ import test_append_sort", stmt="test_append_sort(10000)", number=10**4)
# 122.229761124
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment