Timsort execution time in Python

688 views Asked by At

I'm studying some sorting algorithms and their execution times. I implemented some algorithms in Python and I am measuring how long they take to sort some arrays. I found that Python natively implements Timsort as sorting algorithm for lists. However, I wanted to compare the native Timsort with an implementation I found on GitHub (this one). How is it possible that the native implementation takes 0.000630140304565 seconds to sort an array of 51200 elements while the implementation I linked before takes 40.7546050549 seconds to sort the same array?

[EDIT] To get time I use "time.time()" before and after the execution of the sorting algorithm and then I just make the difference.

I expected the native implementation to be faster, but not so much. The fact is that I have implemented also other sorting algorithms in Python and, for example, Merge-Sort takes 0.148133039474 seconds to sort the same array. I did not expect this big difference between Merge-Sort and the Python implementation of Timsort.

[EDIT2] So the problem is that the implementation I found is not efficient and is not really Timsort. Sorry guys, I just found that Timsort was theta(nlgn) and I believed that was the right implementation. Now the problem is: does an efficient Python implementation of Timsort exist?

1

There are 1 answers

0
timsort_repost On

24 November 2020

A very complete and tested python implementation of timsort from October 2018 (before the original question here was posed) is here:

https://gist.github.com/ruminations/89a045dc0ef7edfb92304a0de0752ee0

The commentary includes useful links for the purpose of education.