Я сделал небольшой тестовый пример для сравнения скорости 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
Я использую PyYAML 3.11 с библиотекой libyaml C на ubuntu 12.04. Я знаю, что json намного проще, чем yaml, но с коэффициентом 223x между json и yaml. Мне интересно, правильна ли моя конфигурация или нет.
У вас есть такое же соотношение скоростей?
Как ускорить yaml.load()
?