json和simplejson Python模块有什么区别?

2024-04-26 04:04:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我看到许多项目使用标准库中的simplejson模块而不是json模块。此外,还有许多不同的simplejson模块。为什么要使用这些替代品,而不是标准库中的替代品?


Tags: 模块项目json替代品标准simplejson
3条回答

jsonissimplejson,添加到stdlib。但是由于json是在2.6中添加的,simplejson具有处理更多Python版本(2.4+)的优势。

simplejson更新的频率也比Python高,因此如果需要(或想要)最新版本,最好尽可能使用simplejson本身。

在我看来,一个好的做法是使用其中一个作为回退。

try:
    import simplejson as json
except ImportError:
    import json

所有这些答案都不是很有帮助,因为它们对时间敏感。

在做了一些我自己的研究之后,我发现simplejson确实比内置的要快,如果保持更新到最新版本。

pip/easy_install想在ubuntu 12.04上安装2.3.2,但是在发现最新的simplejson版本实际上是3.3.0之后,我更新了它并重新运行了时间测试。

  • simplejson在加载时大约比内置的json快3倍
  • simplejson比转储时的内置json快约30%

免责声明:

上面的语句在python-2.7.3和simplejson 3.3.0中(使用c加速) 为了确保我的答案对时间也不敏感,您应该运行自己的测试来检查,因为版本之间的差异太大;没有一个简单的答案对时间不敏感。

如何判断simplejson中是否启用了C加速:

import simplejson
# If this is True, then c speedups are enabled.
print bool(getattr(simplejson, '_speedups', False))

更新:我最近遇到一个名为ujson的库,它在一些基本测试中的执行速度比simplejson快约3倍。

我不得不不同意其他答案:内置库(在Python 2.7中)不一定比json慢。它也没有this annoying unicode bug

以下是一个简单的基准:

import json
import simplejson
from timeit import repeat

NUMBER = 100000
REPEAT = 10

def 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)

以及在我的系统(Python2.7.4,Linux 64位)上的结果:

Complex real world data:
json dumps 1.56666707993 seconds
simplejson dumps 2.25638604164 seconds
json loads 2.71256899834 seconds
simplejson loads 1.29233884811 seconds

Simple data:
json dumps 0.370109081268 seconds
simplejson dumps 0.574181079865 seconds
json loads 0.422876119614 seconds
simplejson loads 0.270955085754 seconds

对于转储,jsonsimplejson快。 对于加载,simplejson更快。

因为我目前正在构建一个web服务,dumps()更为重要,而且使用标准库总是首选。

而且,cjson在过去4年里没有更新,所以我不会碰它。

相关问题 更多 >