小技巧,写一个自定义的deepcopy函数,速度比原装的快。
In [4]: a = [1,2,3] In [5]: from pickle import loads,dumps In [6]: my_deepcopy = lambda x : loads(dumps(x)) In [7]: timeit -n 1000 -r 3 my_deepcopy(a) 982 ns ± 40.5 ns per loop (mean ± std. dev. of 3 runs, 1000 loops each) In [9]: from copy import deepcopy In [10]: timeit -n 1000 -r 3 deepcopy(a) 3.21 µs ± 177 ns per loop (mean ± std. dev. of 3 runs, 1000 loops each)
从上面的代码运行来看,时间快了三分之一,但都是单位尽然到了纳秒级别,是不是我电脑太快了。