我试图找出是否有更快的方法来排序和修改python中的列表,除了原生的"sorted"函数.
100 我有多个来自自定义类的对象,我想根据它们的Z属性对它们进行排序,Z属性是一个整数值.理想情况下,对象存储在字典中,但如果性能更好的话,列表也可以.它们将以每秒60次的速度更新.并且在任何帧,任何对象的任何Z属性都可能改变,并且需要相应地对列表进行排序.可能会不时地添加或删除对象,但优化尤其针对对象的Z值更改后的列表/字典排序.
我还可以一次更新列表中的一个对象或一个Z值更改,并且只更改该对象在排序中的位置,如果有快速的方法这样做的话.
我问heaps个是不是一个很好的解决方案,但他们不是. 在那个链接的帖子中,我得到了关于B树、AVL树、红黑树、跳过列表和Python排序容器的建议,但我不确定它们是否对我的特定用例有帮助.
我try 在列表中循环单个对象,直到它到达正确的Z位置,但这种方法也比使用原生sorted()命令对整个列表进行排序要慢得多.我在下面粘贴了该测试的示例代码.
有没有人知道有没有更快、更有效的方法来对我的特定用例进行排序?如果是这样的话,基本实现的示例代码是什么?
示例代码(this code is merely for performance testing and is not meant to be fully optimized):
import random
from statistics import mean
import time
class Object:
def __init__(self):
self.Z = random.randint(0,1000000)
def __repr__(self):
return str(self.Z)
def __str__(self):
return str(self.Z)
# Creating a sorted list first:
list_base = [Object() for i in range(10000)]
sorted_list = sorted(list_base, key = lambda name: name.Z)
# New object that will be added to the beginning of the list with a higher Z value than the others:
new_obj = Object()
new_obj.Z = 1000001
# Store the timing results in lists for performance testing:
results_native = []
results_custom = []
# Run performance test multiple time:
for i in range(200):
# inserting new object at the beginning of the list:
# current index:
index = 0
sorted_list.insert(index, new_obj)
# Sort method native:
start_time = time.time()
sorted_list = sorted(sorted_list, key = lambda name: name.Z)
finish_time_1 = time.time() - start_time
results_native.append(finish_time_1)
# Inserting the object back to the beginning of the list to repeat the test:
sorted_list.insert(0, sorted_list.pop(-1))
# Sort method Custom looping:
start_time = time.time()
index += 1
while not index >= len(sorted_list) and sorted_list[index].Z <= new_obj.Z:
index += 1
sorted_list.insert(index - 1, sorted_list.pop(0))
finish_time_2 = time.time() - start_time
results_custom.append(finish_time_2)
print("Native finish time:", mean(results_native))
print("Custom loop finish time:", mean(results_custom))