hiện tại tôi đang làm việc để truy xuất hình ảnh bằng Python. Các điểm chính và mô tả được trích xuất từ một hình ảnh trong ví dụ này được thể hiện là numpy.array
s. Hình dạng đầu tiên (2000, 5) và hình dạng thứ hai (2000, 128). Cả hai chỉ chứa giá trị dtype=numpy.float32
.dưa nhanh hơn cPickle với dữ liệu số?
Vì vậy, tôi đã tự hỏi nên sử dụng định dạng nào để lưu các điểm chính và mô tả được trích xuất của tôi. I E. Tôi luôn tiết kiệm 2 tệp: một cho các điểm chính và một cho các bộ mô tả - điều này được tính là một bước trong các phép đo của tôi. Tôi so pickle
, cPickle
(cả với giao thức 0 và 2) và định dạng nhị phân NumPy của .pny
và kết quả được thực sự khó hiểu cho tôi:
Tôi luôn luôn nghĩ rằng cPickle
được coi là nhanh hơn so với các mô-đun pickle
. Nhưng đặc biệt là thời gian tải với giao thức 0 thực sự dính trong kết quả. Có ai có lời giải thích cho điều này không? Có phải vì tôi chỉ sử dụng dữ liệu số không? Có vẻ kỳ lạ ...
PS: Trong mã của tôi Tôi về cơ bản lặp 1000 lần (number=1000
) trên mỗi kỹ thuật và trung bình lần đo cuối cùng:
timer = time.time
print 'npy save...'
t0 = timer()
for i in range(number):
numpy.save(npy_kp_path, kp)
numpy.save(npy_descr_path, descr)
t1 = timer()
results['npy']['save'] = t1 - t0
print 'npy load...'
t0 = timer()
for i in range(number):
kp = numpy.load(npy_kp_path)
descr = numpy.load(npy_descr_path)
t1 = timer()
results['npy']['load'] = t1 - t0
print 'pickle protocol 0 save...'
t0 = timer()
for i in range(number):
with open(pkl0_descr_path, 'wb') as f:
pickle.dump(descr, f, protocol=0)
with open(pkl0_kp_path, 'wb') as f:
pickle.dump(kp, f, protocol=0)
t1 = timer()
results['pkl0']['save'] = t1 - t0
print 'pickle protocol 0 load...'
t0 = timer()
for i in range(number):
with open(pkl0_descr_path, 'rb') as f:
descr = pickle.load(f)
with open(pkl0_kp_path, 'rb') as f:
kp = pickle.load(f)
t1 = timer()
results['pkl0']['load'] = t1 - t0
print 'cPickle protocol 0 save...'
t0 = timer()
for i in range(number):
with open(cpkl0_descr_path, 'wb') as f:
cPickle.dump(descr, f, protocol=0)
with open(cpkl0_kp_path, 'wb') as f:
cPickle.dump(kp, f, protocol=0)
t1 = timer()
results['cpkl0']['save'] = t1 - t0
print 'cPickle protocol 0 load...'
t0 = timer()
for i in range(number):
with open(cpkl0_descr_path, 'rb') as f:
descr = cPickle.load(f)
with open(cpkl0_kp_path, 'rb') as f:
kp = cPickle.load(f)
t1 = timer()
results['cpkl0']['load'] = t1 - t0
print 'pickle highest protocol (2) save...'
t0 = timer()
for i in range(number):
with open(pkl2_descr_path, 'wb') as f:
pickle.dump(descr, f, protocol=pickle.HIGHEST_PROTOCOL)
with open(pkl2_kp_path, 'wb') as f:
pickle.dump(kp, f, protocol=pickle.HIGHEST_PROTOCOL)
t1 = timer()
results['pkl2']['save'] = t1 - t0
print 'pickle highest protocol (2) load...'
t0 = timer()
for i in range(number):
with open(pkl2_descr_path, 'rb') as f:
descr = pickle.load(f)
with open(pkl2_kp_path, 'rb') as f:
kp = pickle.load(f)
t1 = timer()
results['pkl2']['load'] = t1 - t0
print 'cPickle highest protocol (2) save...'
t0 = timer()
for i in range(number):
with open(cpkl2_descr_path, 'wb') as f:
cPickle.dump(descr, f, protocol=cPickle.HIGHEST_PROTOCOL)
with open(cpkl2_kp_path, 'wb') as f:
cPickle.dump(kp, f, protocol=cPickle.HIGHEST_PROTOCOL)
t1 = timer()
results['cpkl2']['save'] = t1 - t0
print 'cPickle highest protocol (2) load...'
t0 = timer()
for i in range(number):
with open(cpkl2_descr_path, 'rb') as f:
descr = cPickle.load(f)
with open(cpkl2_kp_path, 'rb') as f:
kp = cPickle.load(f)
t1 = timer()
results['cpkl2']['load'] = t1 - t0
Tôi vừa mới nhận thấy điều này ngày hôm nay và tìm thấy câu hỏi của bạn, tôi nhận được ít nhất một thứ tự chênh lệch độ lớn. dưa là chắc chắn nhanh hơn so với cpickle. –