简体   繁体   English

Python中的Timsort执行时间

[英]Timsort execution time in Python

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.我在 Python 中实现了一些算法,我正在测量它们对一些数组进行排序所需的时间。 I found that Python natively implements Timsort as sorting algorithm for lists.我发现 Python 本身就将 Timsort 实现为列表的排序算法。 However, I wanted to compare the native Timsort with an implementation I found on GitHub ( this one ).但是,我想将本地 Timsort 与我在 GitHub 上找到的实现( 这个)进行比较。 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?本地实现怎么可能需要 0.000630140304565 秒来对 51200 个元素的数组进行排序,而我之前链接的实现需要 40.7546050549 秒来对同一数组进行排序?

[EDIT] To get time I use "time.time()" before and after the execution of the sorting algorithm and then I just make the difference. [编辑] 为了获得时间,我在执行排序算法之前和之后使用“time.time()”,然后我只是有所作为。

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.事实上,我还在 Python 中实现了其他排序算法,例如,合并排序需要 0.148133039474 秒来对同一数组进行排序。 I did not expect this big difference between Merge-Sort and the Python implementation of Timsort.我没想到 Merge-Sort 和 Timsort 的 Python 实现之间有这么大的区别。

[EDIT2] So the problem is that the implementation I found is not efficient and is not really Timsort. [EDIT2] 所以问题是我发现的实现效率不高,也不是真正的 Timsort。 Sorry guys, I just found that Timsort was theta(nlgn) and I believed that was the right implementation.抱歉,我刚刚发现 Timsort 是 theta(nlgn) 并且我认为这是正确的实现。 Now the problem is: does an efficient Python implementation of Timsort exist?现在的问题是:Timsort 的高效 Python 实现是否存在?

24 November 2020 2020 年 11 月 24 日

A very complete and tested python implementation of timsort from October 2018 (before the original question here was posed) is here: 2018 年 10 月(在提出原始问题之前)的 timsort 的非常完整且经过测试的 python 实现在这里:

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

The commentary includes useful links for the purpose of education.评论包括用于教育目的的有用链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM