python - 排序功能的时间,python 3
问题描述
如果我有一个已经排序的列表,并且我在列表末尾添加了另一个项目(不一定已排序),排序需要多长时间才能再次对该列表进行排序?
list1 = [1, 2, 3, 5]
list1.append(4)
list1.sort()
解决方案
使用 BeamsAdept 的示例,您还可以执行两者来比较性能。
import time
#WITHOUT SORT
start_time = time.time()
list1 = []
n = 100000
for i in range(n):
list1.append(random.randint(1, 1000))
print("Execution time : " + str((time.time() - start_time)))
#SORTING
start_time = time.time()
list1 = []
for i in range(n):
list1.append(random.randint(1, 1000))
list1.sort()
print("Execution time sorting : " + str((time.time() - start_time)))
返回例如:
Execution time : 0.123291015625
Execution time sorting : 58.519288063049316
推荐阅读
- python - 在 Python 中为 Old Macdonald 使用列表和循环
- javascript - 使用 discord.js 为我的 discord 机器人创建 Reaction Roll 函数的最佳方法是什么?
- go - 将时间戳转换为字符串
- server - 出于什么编程原因,物联网编程设备总是需要云/服务器访问?
- javascript - OwlCarousal Camera Slider 猫头鹰项目图像,你如何改变定位左右中心?
- spring-boot - Heroku Spring Boot 应用程序启动问题
- vuetify.js - 将 Vuetify 1.5 数据表升级到 2.0:自定义列、行方式和行内点击事件
- c++ - unique_ptr 与原始指针的不同行为
- xamarin - Xamarin - 如何创建一个布局,在所有设备上调整其大小,同时保持其比例
- python - SQL Server Python 内部错误:应该有标签