python内置json模块的作用_json和simplejson Python模块之间有什么区别?
我不同意其他答案:內置json庫(在Python 2.7中)不一定比它慢simplejson。它也沒有這個討厭的unicode bug。
這是一個簡單的基準:import?jsonimport?simplejsonfrom?timeit?import?repeat
NUMBER?=?100000REPEAT?=?10def?compare_json_and_simplejson(data):
"""Compare?json?and?simplejson?-?dumps?and?loads"""
compare_json_and_simplejson.data?=?data
compare_json_and_simplejson.dump?=?json.dumps(data)
assert?json.dumps(data)?==?simplejson.dumps(data)
result?=?min(repeat("json.dumps(compare_json_and_simplejson.data)",?"from?__main__?import?json,?compare_json_and_simplejson",
repeat?=?REPEAT,?number?=?NUMBER))
print?"??????json?dumps?{}?seconds".format(result)
result?=?min(repeat("simplejson.dumps(compare_json_and_simplejson.data)",?"from?__main__?import?simplejson,?compare_json_and_simplejson",
repeat?=?REPEAT,?number?=?NUMBER))
print?"simplejson?dumps?{}?seconds".format(result)
assert?json.loads(compare_json_and_simplejson.dump)?==?data
result?=?min(repeat("json.loads(compare_json_and_simplejson.dump)",?"from?__main__?import?json,?compare_json_and_simplejson",
repeat?=?REPEAT,?number?=?NUMBER))
print?"??????json?loads?{}?seconds".format(result)
result?=?min(repeat("simplejson.loads(compare_json_and_simplejson.dump)",?"from?__main__?import?simplejson,?compare_json_and_simplejson",
repeat?=?REPEAT,?number?=?NUMBER))
print?"simplejson?loads?{}?seconds".format(result)print?"Complex?real?world?data:"?COMPLEX_DATA?=?{'status':?1,?'timestamp':?1362323499.23,?'site_code':?'testing123',?'remote_address':?'212.179.220.18',?'input_text':?u'ny?monday?for?less?than?\u20aa123',?'locale_value':?'UK',?'eva_version':?'v1.0.3286',?'message':?'Successful?Parse',?'muuid1':?'11e2-8414-a5e9e0fd-95a6-12313913cc26',?'api_reply':?{"api_reply":?{"Money":?{"Currency":?"ILS",?"Amount":?"123",?"Restriction":?"Less"},?"ProcessedText":?"ny?monday?for?less?than?\\u20aa123",?"Locations":?[{"Index":?0,?"Derived?From":?"Default",?"Home":?"Default",?"Departure":?{"Date":?"2013-03-04"},?"Next":?10},?{"Arrival":?{"Date":?"2013-03-04",?"Calculated":?True},?"Index":?10,?"All?Airports?Code":?"NYC",?"Airports":?"EWR,JFK,LGA,PHL",?"Name":?"New?York?City,?New?York,?United?States?(GID=5128581)",?"Latitude":?40.71427,?"Country":?"US",?"Type":?"City",?"Geoid":?5128581,?"Longitude":?-74.00597}]}}}compare_json_and_simplejson(COMPLEX_DATA)print?"\nSimple?data:"SIMPLE_DATA?=?[1,?2,?3,?"asasd",?{'a':'b'}]compare_json_and_simplejson(SIMPLE_DATA)
我的系統(tǒng)上的結果(Python 2.7.4,Linux 64位):復雜的現(xiàn)實世界數(shù)據(jù):
json轉儲1.56666707993秒
simplejson轉儲2.25638604164秒
json加載2.71256899834秒
simplejson加載1.29233884811秒
簡單數(shù)據(jù):
json轉儲0.370109081268秒
simplejson轉儲0.574181079865秒
json加載0.422876119614秒
simplejson加載0.270955085754秒
對于傾銷,json比快simplejson。對于加載,simplejson速度更快。
由于我目前正在構建Web服務,dumps()因此更為重要 - 并且始終首選使用標準庫。
此外,cjson在過去4年沒有更新,所以我不會碰它。
總結
以上是生活随笔為你收集整理的python内置json模块的作用_json和simplejson Python模块之间有什么区别?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 判断点在直线的哪一侧_点与线、线与线、距
- 下一篇: 华硕怎么加固态硬盘装系统 华硕加固态硬盘