【发布时间】:2025-12-22 19:35:06
【问题描述】:
我做了一个小测试用例来比较 YAML 和 JSON 的速度:
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)
结果是:
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
我在 ubuntu 12.04 上使用 PyYAML 3.11 和 libyaml C 库。 我知道 json 比 yaml 简单得多,但是 json 和 yaml 之间的比率是 223 倍,我想知道我的配置是否正确。
你们的速比一样吗?
如何加快yaml.load() 的速度?
【问题讨论】: