python3下拷贝工具调研

结果: pickle 速度最快

 python3 中 pickle 其实就是cpickle

- https://docs.python.org/3.7/l...
- pickle.dumps 有缓存功能,同一个对象的多次dumps, 不会重复计算;Thepicklemodule keeps track of the objects it has already serialized, so that later references to the same object won’t be serialized again.marshaldoesn’t do this.


速度
pickle: 5.236833497881889
cpickle: 5.370606060326099
copy: 32.38146398514509
json: 57.057579030096534
marsha: 6.961339645087719

扩展讨论:
1、pickle的对象追踪并不稳定,目前还不清楚内部的规则

性能测试脚本

import pickle
import _pickle as cPickle
import timeit
import copy
import json
import math
import marshal



a = [{'id': 3021431, 'scheme_id': 1, 'cycle_length': 130, 'offset': 0, 'phases': '[{"ring":0,"barrier":0,"order":1,"position":1,"phase_id":4,"green_time":47,"red_time":0,"yellow_time":3},{"ring":0,"barrier":0,"order":1,"position":1,"phase_id":8,"green_time":47,"red_time":0,"yellow_time":3},{"ring":0,"barrier":1,"order":2,"position":2,"phase_id":4,"green_time":23,"red_time":0,"yellow_time":3},{"ring":0,"barrier":1,"order":2,"position":2,"phase_id":7,"green_time":23,"red_time":0,"yellow_time":3},{"ring":0,"barrier":2,"order":3,"position":3,"phase_id":7,"green_time":17,"red_time":0,"yellow_time":3},{"ring":0,"barrier":3,"order":4,"position":4,"phase_id":1,"green_time":31,"red_time":0,"yellow_time":3}]', 'ring_diagram': '[]'}, {'id': 3021432, 'scheme_id': 2, 'cycle_length': 170, 'offset': 0, 'phases': '[{"ring":0,"barrier":0,"order":1,"position":1,"phase_id":4,"green_time":38,"red_time":0,"yellow_time":3},{"ring":0,"barrier":0,"order":1,"position":1,"phase_id":8,"green_time":38,"red_time":0,"yellow_time":3},{"ring":0,"barrier":1,"order":2,"position":2,"phase_id":4,"green_time":53,"red_time":0,"yellow_time":3},{"ring":0,"barrier":1,"order":2,"position":2,"phase_id":7,"green_time":53,"red_time":0,"yellow_time":3},{"ring":0,"barrier":2,"order":3,"position":3,"phase_id":7,"green_time":40,"red_time":0,"yellow_time":3},{"ring":0,"barrier":3,"order":4,"position":4,"phase_id":1,"green_time":27,"red_time":0,"yellow_time":3}]', 'ring_diagram': '[]'}, {'id': 3021433, 'scheme_id': 3, 'cycle_length': 160, 'offset': 0, 'phases': '[{"ring":0,"barrier":0,"order":1,"position":1,"phase_id":4,"green_time":47,"red_time":0,"yellow_time":3},{"ring":0,"barrier":0,"order":1,"position":1,"phase_id":8,"green_time":47,"red_time":0,"yellow_time":3},{"ring":0,"barrier":1,"order":2,"position":2,"phase_id":4,"green_time":37,"red_time":0,"yellow_time":3},{"ring":0,"barrier":1,"order":2,"position":2,"phase_id":7,"green_time":37,"red_time":0,"yellow_time":3},{"ring":0,"barrier":2,"order":3,"position":3,"phase_id":7,"green_time":22,"red_time":0,"yellow_time":3},{"ring":0,"barrier":3,"order":4,"position":4,"phase_id":1,"green_time":42,"red_time":0,"yellow_time":3}]', 'ring_diagram': '[]'}]

def p1(a):
    return pickle.loads(pickle.dumps(a))

def p2(a):
    return cPickle.loads(cPickle.dumps(a))

def p3(a):
    return copy.deepcopy(a)

def p4(a):
    return json.loads(json.dumps(a))

def p5(a):
    return marshal.loads(marshal.dumps(a))


ret = timeit.repeat('p1(a)', setup='from __main__ import p1, p2, a', number=1000000, globals=globals(), repeat=5)
print(type(ret))
print('pickle:', sum(ret)/len(ret))
ret = timeit.repeat('p2(a)', setup='from __main__ import p1, p2, a', number=1000000, globals=globals(), repeat=5)
print('cpickle:', sum(ret)/len(ret))
ret = timeit.repeat('p3(a)', setup='from __main__ import p1, p2, a', number=1000000, globals=globals(), repeat=5)
print('copy:', sum(ret)/len(ret))
ret = timeit.repeat('p4(a)', setup='from __main__ import p1, p2, a', number=1000000, globals=globals(), repeat=5)
print('json:', sum(ret)/len(ret))
ret = timeit.repeat('p5(a)', setup='from __main__ import p1, p2, a', number=1000000, globals=globals(), repeat=5)
print('marsha:', sum(ret)/len(ret))

你可能感兴趣的:(python3)