I have seen many projects using simplejson
module instead of json
module from the Standard Library. Also, there are many different simplejson
modules. Why would use these alternatives, instead of the one in the Standard Library?
我从标准库中看到了许多使用simplejson模块而不是json模块的项目。此外,还有许多不同的simplejson模块。为什么要使用这些替代方法,而不是标准库中的一个呢?
12 个解决方案
#1
349
json
is simplejson
, added to the stdlib. But since json
was added in 2.6, simplejson
has the advantage of working on more Python versions (2.4+).
json是simplejson,添加到stdlib中。但是由于在2.6中添加了json, simplejson的优点是可以使用更多的Python版本(2.4+)。
simplejson
is also updated more frequently than Python, so if you need (or want) the latest version, it's best to use simplejson
itself, if possible.
simplejson也比Python更新得更频繁,所以如果您需要(或想要)最新版本,最好是使用simplejson本身。
A good practice, in my opinion, is to use one or the other as a fallback.
在我看来,一个好的做法是使用一个或另一个作为后备。
try: import simplejson as json
except ImportError: import json
#2
76
I have to disagree with the other answers: the built in json
library (in Python 2.7) is not necessarily slower than simplejson
. It also doesn't have this annoying unicode bug.
我不得不不同意其他的答案:在json库中构建的(在Python 2.7中)并不一定比simplejson慢。它也没有这种恼人的unicode bug。
Here is a simple benchmark:
这里有一个简单的基准:
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)
And the results on my system (Python 2.7.4, Linux 64-bit):
我的系统的结果(Python 2.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复杂的真实世界数据:json转储1.56666707993秒simplejson转储2.25638604164秒json负载2.71256899834秒simplejson加载1.29233884811秒。
Simple data:
json dumps 0.370109081268 seconds
simplejson dumps 0.574181079865 seconds
json loads 0.422876119614 seconds
simplejson loads 0.270955085754 seconds简单数据:json转储0.370109081268秒simplejson转储0.574181079865秒json加载0.422876119614秒simplejson加载0.270955085754秒。
For dumping, json
is faster than simplejson
. For loading, simplejson
is faster.
对于转储,json比simplejson更快。对于加载,simplejson更快。
Since I am currently building a web service, dumps()
is more important—and using a standard library is always preferred.
由于我目前正在构建一个web服务,dump()更重要,而且使用标准库总是首选。
Also, cjson
was not updated in the past 4 years, so I wouldn't touch it.
另外,cjson在过去的4年里没有更新过,所以我不会去碰它。
#3
20
All of these answers aren't very helpful because they are time sensitive.
所有这些答案都不是很有用,因为它们对时间很敏感。
After doing some research of my own I found that simplejson
is indeed faster than the builtin, if you keep it updated to the latest version.
在做了一些我自己的研究之后,我发现simplejson确实比内置的更快,如果你把它更新到最新版本。
pip/easy_install
wanted to install 2.3.2 on ubuntu 12.04, but after finding out the latest simplejson
version is actually 3.3.0, so I updated it and reran the time tests.
pip/easy_install希望在ubuntu 12.04上安装2.3.2,但是在找到最新的simplejson版本后,实际上是3.3.0,所以我更新了它并重新运行了时间测试。
-
simplejson
is about 3x faster than the builtinjson
at loads - simplejson的速度比内置的json要快3倍。
-
simplejson
is about 30% faster than the builtinjson
at dumps - simplejson的速度比内置的json快30%。
Disclaimer:
The above statements are in python-2.7.3 and simplejson 3.3.0 (with c speedups) And to make sure my answer also isn't time sensitive, you should run your own tests to check since it varies so much between versions; there's no easy answer that isn't time sensitive.
上面的语句是python-2.7.3和simplejson 3.3.0(使用c语言的速度),并且确保我的答案也不是时间敏感的,您应该运行您自己的测试来检查,因为版本之间差异很大;没有一个简单的答案是时间敏感的。
How to tell if C speedups are enabled in simplejson:
import simplejson
# If this is True, then c speedups are enabled.
print bool(getattr(simplejson, '_speedups', False))
UPDATE: I recently came across a library called ujson that is performing ~3x faster than simplejson
with some basic tests.
更新:我最近遇到了一个名为ujson的库,它执行的速度比simplejson要快,而且还做了一些基本的测试。
#4
19
I've been benchmarking json, simplejson and cjson.
我一直在对json、simplejson和cjson进行基准测试。
- cjson is fastest
- cjson是最快的
- simplejson is almost on par with cjson
- simplejson几乎与cjson相同。
- json is about 10x slower than simplejson
- json比simplejson慢10倍。
http://pastie.org/1507411:
$ python test_serialization_speed.py
--------------------
Encoding Tests
--------------------
Encoding: 100000 x {'m': 'asdsasdqwqw', 't': 3}
[ json] 1.12385 seconds for 100000 runs. avg: 0.011239ms
[simplejson] 0.44356 seconds for 100000 runs. avg: 0.004436ms
[ cjson] 0.09593 seconds for 100000 runs. avg: 0.000959ms
Encoding: 10000 x {'m': [['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19]], 't': 3}
[ json] 7.76628 seconds for 10000 runs. avg: 0.776628ms
[simplejson] 0.51179 seconds for 10000 runs. avg: 0.051179ms
[ cjson] 0.44362 seconds for 10000 runs. avg: 0.044362ms
--------------------
Decoding Tests
--------------------
Decoding: 100000 x {"m": "asdsasdqwqw", "t": 3}
[ json] 3.32861 seconds for 100000 runs. avg: 0.033286ms
[simplejson] 0.37164 seconds for 100000 runs. avg: 0.003716ms
[ cjson] 0.03893 seconds for 100000 runs. avg: 0.000389ms
Decoding: 10000 x {"m": [["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19]], "t": 3}
[ json] 37.26270 seconds for 10000 runs. avg: 3.726270ms
[simplejson] 0.56643 seconds for 10000 runs. avg: 0.056643ms
[ cjson] 0.33007 seconds for 10000 runs. avg: 0.033007ms
#5
6
An API incompatibility I found, with Python 2.7 vs simplejson 3.3.1 is in whether output produces str or unicode objects. e.g.
我发现了一个API不兼容,Python 2.7和simplejson 3.3.1是在输出是否产生str或unicode对象的情况下发现的。如。
>>> from json import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{u'a': u'b'}
vs
vs
>>> from simplejson import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{'a': 'b'}
If the preference is to use simplejson, then this can be addressed by coercing the argument string to unicode, as in:
如果首选项是使用simplejson,那么可以通过将参数字符串强制转换为unicode来解决:
>>> from simplejson import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode(unicode("""{ "a":"b" }""", "utf-8"))
{u'a': u'b'}
The coercion does require knowing the original charset, for example:
强迫确实需要知道原始的字符集,例如:
>>> jd.decode(unicode("""{ "a": "ξηθννββωφρες" }"""))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 8: ordinal not in range(128)
This is the won't fix issue 40
这是不解决问题的40。
#6
5
The builtin json
module got included in Python 2.6. Any projects that support versions of Python < 2.6 need to have a fallback. In many cases, that fallback is simplejson
.
构建的json模块包含在Python 2.6中。任何支持Python版本< 2.6版本的项目都需要有一个后备方案。在许多情况下,回退是simplejson。
#7
5
Another reason projects use simplejson is that the builtin json did not originally include its C speedups, so the performance difference was noticeable.
项目使用simplejson的另一个原因是,builtin json最初并不包括它的C语言,所以性能差异是显而易见的。
#8
4
Here's (a now outdated) comparison of Python json libraries:
下面是Python json库的比较:
Comparing JSON modules for Python (archive link)
比较Python的JSON模块(archive link)
Regardless of the results in this comparison you should use the standard library json if you are on Python 2.6. And.. might as well just use simplejson otherwise.
不管这个比较结果如何,如果您使用的是Python 2.6,那么您应该使用标准库json。和. .也可以使用simplejson。
#9
2
simplejson module is simply 1,5 times faster than json (On my computer, with simplejson 2.1.1 and Python 2.7 x86).
simplejson模块的速度比json快1到5倍(在我的计算机上,使用simplejson 2.1.1和Python 2.7 x86)。
If you want, you can try the benchmark: http://abral.altervista.org/jsonpickle-bench.zip On my PC simplejson is faster than cPickle. I would like to know also your benchmarks!
如果您愿意,可以尝试一下基准:http://abral.altervista.org/jsonpick-bench.zip在我的PC simplejson上比cPickle更快。我想知道你的基准!
Probably, as said Coady, the difference between simplejson and json is that simplejson includes _speedups.c. So, why don't python developers use simplejson?
可能,正如Coady所说,simplejson和json的区别在于simplejson包含_speedups.c。那么,为什么python开发人员不使用simplejson呢?
#10
2
Some values are serialized differently between simplejson and json.
一些值在simplejson和json之间有不同的序列化。
Notably, instances of collections.namedtuple
are serialized as arrays by json
but as objects by simplejson
. You can override this behaviour by passing namedtuple_as_object=False
to simplejson.dump
, but by default the behaviours do not match.
值得注意的是,collections.namedtuple是由json序列化为数组,而以simplejson作为对象。您可以通过传递namedtuple_as_object=False来覆盖该行为。转储,但默认情况下,行为不匹配。
>>> import collections, simplejson, json
>>> TupleClass = collections.namedtuple("TupleClass", ("a", "b"))
>>> value = TupleClass(1, 2)
>>> json.dumps(value)
'[1, 2]'
>>> simplejson.dumps(value)
'{"a": 1, "b": 2}'
>>> simplejson.dumps(value, namedtuple_as_object=False)
'[1, 2]'
#11
1
In python3, if you a string of b'bytes'
, with json
you have to .decode()
the content before you can load it. simplejson
takes care of this so you can just do simplejson.loads(byte_string)
.
在python3中,如果你有一个字符串b'bytes',用json你必须。decode()在你可以加载它之前。simplejson解决了这个问题,所以您可以只执行simplejson.load (byte_string)。
#12
0
I came across this question as I was looking to install simplejson for Python 2.6. I needed to use the 'object_pairs_hook' of json.load() in order to load a json file as an OrderedDict. Being familiar with more recent versions of Python I didn't realize that the json module for Python 2.6 doesn't include the 'object_pairs_hook' so I had to install simplejson for this purpose. From personal experience this is why i use simplejson as opposed to the standard json module.
当我想为Python 2.6安装simplejson时,我遇到了这个问题。我需要使用json.load()的“object_pairs_hook”来加载一个json文件作为OrderedDict。由于熟悉Python的最新版本,我没有意识到Python 2.6的json模块不包括“object_pairs_hook”,因此我必须为这个目的安装simplejson。从个人经验来看,这就是为什么我使用simplejson而不是标准的json模块。
#1
349
json
is simplejson
, added to the stdlib. But since json
was added in 2.6, simplejson
has the advantage of working on more Python versions (2.4+).
json是simplejson,添加到stdlib中。但是由于在2.6中添加了json, simplejson的优点是可以使用更多的Python版本(2.4+)。
simplejson
is also updated more frequently than Python, so if you need (or want) the latest version, it's best to use simplejson
itself, if possible.
simplejson也比Python更新得更频繁,所以如果您需要(或想要)最新版本,最好是使用simplejson本身。
A good practice, in my opinion, is to use one or the other as a fallback.
在我看来,一个好的做法是使用一个或另一个作为后备。
try: import simplejson as json
except ImportError: import json
#2
76
I have to disagree with the other answers: the built in json
library (in Python 2.7) is not necessarily slower than simplejson
. It also doesn't have this annoying unicode bug.
我不得不不同意其他的答案:在json库中构建的(在Python 2.7中)并不一定比simplejson慢。它也没有这种恼人的unicode bug。
Here is a simple benchmark:
这里有一个简单的基准:
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)
And the results on my system (Python 2.7.4, Linux 64-bit):
我的系统的结果(Python 2.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复杂的真实世界数据:json转储1.56666707993秒simplejson转储2.25638604164秒json负载2.71256899834秒simplejson加载1.29233884811秒。
Simple data:
json dumps 0.370109081268 seconds
simplejson dumps 0.574181079865 seconds
json loads 0.422876119614 seconds
simplejson loads 0.270955085754 seconds简单数据:json转储0.370109081268秒simplejson转储0.574181079865秒json加载0.422876119614秒simplejson加载0.270955085754秒。
For dumping, json
is faster than simplejson
. For loading, simplejson
is faster.
对于转储,json比simplejson更快。对于加载,simplejson更快。
Since I am currently building a web service, dumps()
is more important—and using a standard library is always preferred.
由于我目前正在构建一个web服务,dump()更重要,而且使用标准库总是首选。
Also, cjson
was not updated in the past 4 years, so I wouldn't touch it.
另外,cjson在过去的4年里没有更新过,所以我不会去碰它。
#3
20
All of these answers aren't very helpful because they are time sensitive.
所有这些答案都不是很有用,因为它们对时间很敏感。
After doing some research of my own I found that simplejson
is indeed faster than the builtin, if you keep it updated to the latest version.
在做了一些我自己的研究之后,我发现simplejson确实比内置的更快,如果你把它更新到最新版本。
pip/easy_install
wanted to install 2.3.2 on ubuntu 12.04, but after finding out the latest simplejson
version is actually 3.3.0, so I updated it and reran the time tests.
pip/easy_install希望在ubuntu 12.04上安装2.3.2,但是在找到最新的simplejson版本后,实际上是3.3.0,所以我更新了它并重新运行了时间测试。
-
simplejson
is about 3x faster than the builtinjson
at loads - simplejson的速度比内置的json要快3倍。
-
simplejson
is about 30% faster than the builtinjson
at dumps - simplejson的速度比内置的json快30%。
Disclaimer:
The above statements are in python-2.7.3 and simplejson 3.3.0 (with c speedups) And to make sure my answer also isn't time sensitive, you should run your own tests to check since it varies so much between versions; there's no easy answer that isn't time sensitive.
上面的语句是python-2.7.3和simplejson 3.3.0(使用c语言的速度),并且确保我的答案也不是时间敏感的,您应该运行您自己的测试来检查,因为版本之间差异很大;没有一个简单的答案是时间敏感的。
How to tell if C speedups are enabled in simplejson:
import simplejson
# If this is True, then c speedups are enabled.
print bool(getattr(simplejson, '_speedups', False))
UPDATE: I recently came across a library called ujson that is performing ~3x faster than simplejson
with some basic tests.
更新:我最近遇到了一个名为ujson的库,它执行的速度比simplejson要快,而且还做了一些基本的测试。
#4
19
I've been benchmarking json, simplejson and cjson.
我一直在对json、simplejson和cjson进行基准测试。
- cjson is fastest
- cjson是最快的
- simplejson is almost on par with cjson
- simplejson几乎与cjson相同。
- json is about 10x slower than simplejson
- json比simplejson慢10倍。
http://pastie.org/1507411:
$ python test_serialization_speed.py
--------------------
Encoding Tests
--------------------
Encoding: 100000 x {'m': 'asdsasdqwqw', 't': 3}
[ json] 1.12385 seconds for 100000 runs. avg: 0.011239ms
[simplejson] 0.44356 seconds for 100000 runs. avg: 0.004436ms
[ cjson] 0.09593 seconds for 100000 runs. avg: 0.000959ms
Encoding: 10000 x {'m': [['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19]], 't': 3}
[ json] 7.76628 seconds for 10000 runs. avg: 0.776628ms
[simplejson] 0.51179 seconds for 10000 runs. avg: 0.051179ms
[ cjson] 0.44362 seconds for 10000 runs. avg: 0.044362ms
--------------------
Decoding Tests
--------------------
Decoding: 100000 x {"m": "asdsasdqwqw", "t": 3}
[ json] 3.32861 seconds for 100000 runs. avg: 0.033286ms
[simplejson] 0.37164 seconds for 100000 runs. avg: 0.003716ms
[ cjson] 0.03893 seconds for 100000 runs. avg: 0.000389ms
Decoding: 10000 x {"m": [["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19]], "t": 3}
[ json] 37.26270 seconds for 10000 runs. avg: 3.726270ms
[simplejson] 0.56643 seconds for 10000 runs. avg: 0.056643ms
[ cjson] 0.33007 seconds for 10000 runs. avg: 0.033007ms
#5
6
An API incompatibility I found, with Python 2.7 vs simplejson 3.3.1 is in whether output produces str or unicode objects. e.g.
我发现了一个API不兼容,Python 2.7和simplejson 3.3.1是在输出是否产生str或unicode对象的情况下发现的。如。
>>> from json import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{u'a': u'b'}
vs
vs
>>> from simplejson import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{'a': 'b'}
If the preference is to use simplejson, then this can be addressed by coercing the argument string to unicode, as in:
如果首选项是使用simplejson,那么可以通过将参数字符串强制转换为unicode来解决:
>>> from simplejson import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode(unicode("""{ "a":"b" }""", "utf-8"))
{u'a': u'b'}
The coercion does require knowing the original charset, for example:
强迫确实需要知道原始的字符集,例如:
>>> jd.decode(unicode("""{ "a": "ξηθννββωφρες" }"""))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 8: ordinal not in range(128)
This is the won't fix issue 40
这是不解决问题的40。
#6
5
The builtin json
module got included in Python 2.6. Any projects that support versions of Python < 2.6 need to have a fallback. In many cases, that fallback is simplejson
.
构建的json模块包含在Python 2.6中。任何支持Python版本< 2.6版本的项目都需要有一个后备方案。在许多情况下,回退是simplejson。
#7
5
Another reason projects use simplejson is that the builtin json did not originally include its C speedups, so the performance difference was noticeable.
项目使用simplejson的另一个原因是,builtin json最初并不包括它的C语言,所以性能差异是显而易见的。
#8
4
Here's (a now outdated) comparison of Python json libraries:
下面是Python json库的比较:
Comparing JSON modules for Python (archive link)
比较Python的JSON模块(archive link)
Regardless of the results in this comparison you should use the standard library json if you are on Python 2.6. And.. might as well just use simplejson otherwise.
不管这个比较结果如何,如果您使用的是Python 2.6,那么您应该使用标准库json。和. .也可以使用simplejson。
#9
2
simplejson module is simply 1,5 times faster than json (On my computer, with simplejson 2.1.1 and Python 2.7 x86).
simplejson模块的速度比json快1到5倍(在我的计算机上,使用simplejson 2.1.1和Python 2.7 x86)。
If you want, you can try the benchmark: http://abral.altervista.org/jsonpickle-bench.zip On my PC simplejson is faster than cPickle. I would like to know also your benchmarks!
如果您愿意,可以尝试一下基准:http://abral.altervista.org/jsonpick-bench.zip在我的PC simplejson上比cPickle更快。我想知道你的基准!
Probably, as said Coady, the difference between simplejson and json is that simplejson includes _speedups.c. So, why don't python developers use simplejson?
可能,正如Coady所说,simplejson和json的区别在于simplejson包含_speedups.c。那么,为什么python开发人员不使用simplejson呢?
#10
2
Some values are serialized differently between simplejson and json.
一些值在simplejson和json之间有不同的序列化。
Notably, instances of collections.namedtuple
are serialized as arrays by json
but as objects by simplejson
. You can override this behaviour by passing namedtuple_as_object=False
to simplejson.dump
, but by default the behaviours do not match.
值得注意的是,collections.namedtuple是由json序列化为数组,而以simplejson作为对象。您可以通过传递namedtuple_as_object=False来覆盖该行为。转储,但默认情况下,行为不匹配。
>>> import collections, simplejson, json
>>> TupleClass = collections.namedtuple("TupleClass", ("a", "b"))
>>> value = TupleClass(1, 2)
>>> json.dumps(value)
'[1, 2]'
>>> simplejson.dumps(value)
'{"a": 1, "b": 2}'
>>> simplejson.dumps(value, namedtuple_as_object=False)
'[1, 2]'
#11
1
In python3, if you a string of b'bytes'
, with json
you have to .decode()
the content before you can load it. simplejson
takes care of this so you can just do simplejson.loads(byte_string)
.
在python3中,如果你有一个字符串b'bytes',用json你必须。decode()在你可以加载它之前。simplejson解决了这个问题,所以您可以只执行simplejson.load (byte_string)。
#12
0
I came across this question as I was looking to install simplejson for Python 2.6. I needed to use the 'object_pairs_hook' of json.load() in order to load a json file as an OrderedDict. Being familiar with more recent versions of Python I didn't realize that the json module for Python 2.6 doesn't include the 'object_pairs_hook' so I had to install simplejson for this purpose. From personal experience this is why i use simplejson as opposed to the standard json module.
当我想为Python 2.6安装simplejson时,我遇到了这个问题。我需要使用json.load()的“object_pairs_hook”来加载一个json文件作为OrderedDict。由于熟悉Python的最新版本,我没有意识到Python 2.6的json模块不包括“object_pairs_hook”,因此我必须为这个目的安装simplejson。从个人经验来看,这就是为什么我使用simplejson而不是标准的json模块。