tôi đã thực hiện một trường hợp thử nghiệm nhỏ để so sánh YAML và JSON tốc độ:Tôi có thể tăng tốc YAML không?
import json
import yaml
from datetime import datetime
from random import randint
NB_ROW=1024
print 'Does yaml is using libyaml ? ',yaml.__with_libyaml__ and 'yes' or 'no'
dummy_data = [ { 'dummy_key_A_%s' % i: i, 'dummy_key_B_%s' % i: i } for i in xrange(NB_ROW) ]
with open('perf_json_yaml.yaml','w') as fh:
t1 = datetime.now()
yaml.safe_dump(dummy_data, fh, encoding='utf-8', default_flow_style=False)
t2 = datetime.now()
dty = (t2 - t1).total_seconds()
print 'Dumping %s row into a yaml file : %s' % (NB_ROW,dty)
with open('perf_json_yaml.json','w') as fh:
t1 = datetime.now()
json.dump(dummy_data,fh)
t2 = datetime.now()
dtj = (t2 - t1).total_seconds()
print 'Dumping %s row into a json file : %s' % (NB_ROW,dtj)
print "json is %dx faster for dumping" % (dty/dtj)
with open('perf_json_yaml.yaml') as fh:
t1 = datetime.now()
data = yaml.safe_load(fh)
t2 = datetime.now()
dty = (t2 - t1).total_seconds()
print 'Loading %s row from a yaml file : %s' % (NB_ROW,dty)
with open('perf_json_yaml.json') as fh:
t1 = datetime.now()
data = json.load(fh)
t2 = datetime.now()
dtj = (t2 - t1).total_seconds()
print 'Loading %s row into from json file : %s' % (NB_ROW,dtj)
print "json is %dx faster for loading" % (dty/dtj)
Và kết quả là:
Does yaml is using libyaml ? yes
Dumping 1024 row into a yaml file : 0.251139
Dumping 1024 row into a json file : 0.007725
json is 32x faster for dumping
Loading 1024 row from a yaml file : 0.401224
Loading 1024 row into from json file : 0.001793
json is 223x faster for loading
Tôi đang sử dụng PyYAML 3.11 với thư viện libyaml C trên ubuntu 12.04. Tôi biết rằng json đơn giản hơn nhiều so với yaml, nhưng với tỷ lệ 223x giữa json và yaml tôi tự hỏi liệu cấu hình của tôi có đúng hay không.
Bạn có cùng tỷ lệ tốc độ không?
Làm cách nào để tăng tốc độ yaml.load()
?
tải vẫn chậm hơn 12x với mẫu yaml.my là danh sách 600.000 từ điển trống. Yaml không cần phải làm bất cứ điều gì thêm ngoại trừ phân tích cú pháp một chút thông minh mà nên mất gần như không có thêm thời gian. – codeshot
Trên mac: brew install yaml-cpp libyaml –
Jivan bạn là một huyền thoại đẫm máu. Tôi sẽ viết lại một số mã python trong C++ để tăng tốc độ. Tập tin yaml 6MB của tôi mất 53 giây để tải bằng cách sử dụng bộ tải yaml tiêu chuẩn, và chỉ 3 giây với CLoader. – nevelis