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

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


jsonsimplejson ,添加到stdlib中。 但是由于json是在2.6中添加的,所以simplejson的优势在于可以使用更多的Python版本(2.4+)。

simplejson也比Python更新更频繁,所以如果你需要(或者想要)最新版本的话,如果可能的话,最好使用simplejson本身。

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

try: import simplejson as json
except ImportError: import json

我不同意其他答案:内置的json库(在Python 2.7中)不一定比simplejson慢。 它也没有这个令人讨厌的unicode错误。

这是一个简单的基准:

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)

结果在我的系统上(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秒

对于倾销, jsonsimplejson更快。 对于加载, simplejson更快。

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

另外, cjson在过去的四年中没有更新,所以我不会去碰它。


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

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

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

  • simplejson比内置json在加载时快大约3倍
  • simplejson比dump中的builtin 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倍,并进行了一些基本测试。

    链接地址: http://www.djcxy.com/p/47137.html

    上一篇: What are the differences between json and simplejson Python modules?

    下一篇: Replace whole line containing a string using Sed