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)

我的系统上的结果(Python 2.7.4,Linux 64位):复杂的现实世界数据:

json转储1.56666707993秒

simplejson转储2.25638604164秒

json加载2.71256899834秒

simplejson加载1.29233884811秒

简单数据:

json转储0.370109081268秒

simplejson转储0.574181079865秒

json加载0.422876119614秒

simplejson加载0.270955085754秒

对于倾销,json比快simplejson。对于加载,simplejson速度更快。

由于我目前正在构建Web服务,dumps()因此更为重要 - 并且始终首选使用标准库。

此外,cjson在过去4年没有更新,所以我不会碰它。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值