質問

私は多くのプロジェクトを使用しているのを見てきました simplejson の代わりにモジュール json 標準ライブラリからのモジュール。また、多くの違いがあります simplejson モジュール。標準的なライブラリの代わりに、これらの選択肢を使用するのはなぜですか?

役に立ちましたか?

解決

json simplejson, 、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. 。また持っていません この迷惑なユニコードバグ.

これが簡単なベンチマークです:

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秒です

ダンピングのために、 json より速いです simplejson。読み込みのために、 simplejson より速いです。

私は現在Webサービスを構築しているので、 dumps() より重要であり、標準的なライブラリを使用することが常に推奨されます。

また、 cjson 過去4年間は更新されていなかったので、触れません。

これらの答えはすべて、彼らは 時間に敏感です.

自分の研究をした後、私はそれを見つけました simplejson 確かにビルトインよりも速いです、 もしも 最新バージョンに更新します。

pip/easy_install Ubuntu 12.04に2.3.2をインストールしたかったのですが、最新情報を見つけた後 simplejson バージョンは実際には3.3.0なので、更新して時間テストを再生しました。

  • simplejson ビルトインよりも約3倍高速です json 負荷で
  • simplejson ビルトインよりも約30%高速です json ダンプで

免責事項:

上記のステートメントはPython-2.7.3およびSimpleJson 3.3.0(C Speedupsを使用)にあり、私の答えも時間に敏感でないことを確認するには、 独自のテストを実行します バージョン間で大きく異なるため、確認する。時間に敏感ではない簡単な答えはありません。

simplejsonでcスピードアップが有効になっているかどうかを判断する方法:

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

アップデート: 私は最近、呼ばれる図書館に出会いました ウジソン これは、より速いパフォーマンスを速くしています simplejson いくつかの基本的なテストで。

私はJSON、SimpleJson、CJSONのベンチマークを行ってきました。

  • CJSONは最速です
  • SimpleJsonはCJSONとほぼ同等です
  • 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

一部の値は、SimpleJsonとJSONの間では異なる方法でシリアル化されています。

特に、のインスタンス collections.namedtuple 配列としてシリアル化されています json しかし、オブジェクトとして simplejson. 。合格することでこの動作をオーバーライドできます namedtuple_as_object=Falsesimplejson.dump, 、しかし、デフォルトでは動作は一致しません。

>>> 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]'

Python 2.7 vs SimpleJson 3.3.1を使用して、私が見つけたAPIの非互換性が、出力がSTRオブジェクトまたはUnicodeオブジェクトを生成するかどうかです。例えば

>>> from json import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{u'a': u'b'}

vs

>>> from simplejson import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{'a': 'b'}

優先がSimpleJSONを使用することである場合、これは次のように引数文字列をUnicodeに強制することで対処できます。

>>> from simplejson import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode(unicode("""{ "a":"b" }""", "utf-8"))
{u'a': u'b'}

強制では、元の憲章を知る必要があります。たとえば、

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

これは修正されません 第40号

ビルトイン json モジュールはPython 2.6に含まれました。 Python <2.6のバージョンをサポートするプロジェクトでは、フォールバックが必要です。多くの場合、そのフォールバックはそうです simplejson.

プロジェクトがSimpleJSONを使用するもう1つの理由は、ビルトインJSONが元々Cスピードアップを含めなかったため、パフォーマンスの違いが顕著であったことです。

これが(現在は時代遅れの)Python JSONライブラリの比較です。

PythonのJSONモジュールの比較 (アーカイブリンク)

この比較の結果に関係なく、Python 2.6を使用している場合は、標準ライブラリJSONを使用する必要があります。そして..それ以外の場合はSimpleJsonを使用するだけでもあります。

SimpleJsonモジュールは、JSONよりも1,5倍高速です(私のコンピューター上で、SimpleJSON 2.1.1およびPython 2.7 X86を使用)。

必要に応じて、ベンチマークを試すことができます。 http://abral.altervista.org/jsonpickle-bench.zip私のPCでは、simplejsonはcpickleよりも速いです。あなたのベンチマークも知りたいです!

おそらく、Coadyが言ったように、SimpleJsonとJsonの違いは、SimpleJsonに_speedups.cが含まれていることです。それでは、なぜPython開発者はSimpleJsonを使用しないのですか?

Python3では、あなたが一連の弦の場合 b'bytes', 、 と json 必ず .decode() ロードする前にコンテンツ。 simplejson あなたができるようにこれを世話してください simplejson.loads(byte_string).

Python 2.6のSimpleJsonをインストールしようとしていたので、この質問に出会いました。 JSONファイルをOrderedDictとしてロードするには、json.load()の「object_pairs_hook」を使用する必要がありました。 Pythonのより最近のバージョンに精通しているため、Python 2.6用のJSONモジュールには「object_pairs_hook」が含まれていないことに気付きませんでした。この目的のためにSimpleJsonをインストールする必要がありました。個人的な経験から、これが標準のJSONモジュールとは対照的にSimpleJsonを使用する理由です。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top