У меня есть скрипт для демонстрации:
#!/usr/bin/env python3
import time
import copy
def reset():
a = [[{'1': [5, 2, 6, 7, 9], '2': 6, '3': 5}], [{'1': [5, 2, 6, 7, 9], '2': 6, '3': 5}]]
b = []
return a, b
a, b = reset()
start = time.time()
b = list(a)
end = time.time()
print("list(a) took:", end-start)
a[0][0]['1'] = [1, 2, 3, 4, 5]
print("b changed with a" if b == a else "b did not change with a")
a, b = reset()
start = time.time()
b = a[:]
end = time.time()
print("a[:] took:", end-start)
a[0][0]['1'] = [1, 2, 3, 4, 5]
print("b changed with a" if b == a else "b did not change with a")
a, b = reset()
start = time.time()
b = copy.copy(a)
end = time.time()
print("copy(a) took:", end-start)
a[0][0]['1'] = [1, 2, 3, 4, 5]
print("b changed with a" if b == a else "b did not change with a")
a, b = reset()
start = time.time()
b = copy.deepcopy(a)
end = time.time()
print("deepcopy(a) took:", end-start)
a[0][0]['1'] = [1, 2, 3, 4, 5]
print("b changed with a" if b == a else "b did not change with a")
И вывод:
list(a) took: 1.1920928955078125e-06
b changed with a
a[:] took: 7.152557373046875e-07
b changed with a
copy(a) took: 3.337860107421875e-06
b changed with a
deepcopy(a) took: 2.5510787963867188e-05
b did not change with a
Нет ли более быстрого способа глубокой копии? Я передаю список функции, которая затем видоизменяется. Я хочу сохранить копию списка, прежде чем он был изменен для сравнения двух, но deepcopy
слишком медленный. Я имею в виду выполнение 1 с против 15 с.
Пример моих данных приведен ниже:
[[{'w': [0.5372377247650572, 1.9111341091016385, -3.2165806256024116, -1.7154987465370053, 1.0917999534858416], 'o': 0.0004326739879156587, 'd': 3.586499431857422e-05}],[{'w': [7.298542669399767, -3.9021024252822105], 'o': 0.019860841402923542, 'd': 0.00105997759946847}, {'w': [-2.8024625186056764, -0.34819658506990847], 'o': 0.4135257109795849, 'd': -0.0016469874583619935}, {'w': [-6.018257518762189, 0.3317488378886934], 'o': 0.5815513019444986, 'd': -1.1787471334339458e-05}]]