Python集排序,为什么这么快?

时间:2014-03-21 06:18:52

标签: python sorting

为什么Python内置sorted的速度如此之快?

我的意思是,非常快,我使用了一组80000个条目,它需要非常短的时间(0.0是time.clock()差异的输出)。 这是某种优化吗?

记录是否已在集合中排序?

顺便说一句,如何对集合进行排序?你能给我代码吗?

1 个答案:

答案 0 :(得分:3)

没有特别的魔力:排序是在C中有效实现的。 time.clock不是对Python代码进行基准测试的理想方法,因为在某些平台上,其输出的分辨率可能非常低。为获得最佳效果,请使用timeit模块测量已用时间。

也没有特殊的算法来排序集合。 sorted内置的setdef sorted(iterable): templist = list(iterable) templist.sort() return templist (或其他任何内容)上调用时,相当于:

{{1}}

所以,真正的魔力是in the list.sort method。 (详细解释了实施in the adjacent file.

相关问题