I have two python functions for sorting a list of integers contained as strings. They are follows:
import random
n = 10000000
unsorted = [str(x) for x in range(n)]
random.shuffle(unsorted)
def sort_alg1(unsorted):
unsorted.sort(key = lambda x: int(x))
return unsorted
def sort_alg2(unsorted):
l=list(map(int,unsorted))
l.sort()
s=list(map(str,l))
return s
print(sort_alg1(unsorted))
print(sort_alg2(unsorted))
Both work as expected. However, according to my profiler (I am using the ever-popular line_profiler by Robert Kern), the first function i.e. sort_alg1
is more than 3 times slower in execution as sort_alg2
. Now that wouldn't be a big problem if I could pinpoint the reason for this, but I am unable to. I have tried looking up differences in the built-in sort()
method and map()
function, lambda, etc. all to no avail. If someone could educate me as to why this is happening, that would be really great!