Docker lxml

Python JSON vs Pickle

cr0hn
2 min readJun 1, 2020

When we need to serialize data we must consider different types of serializers. Python developers have some of them build in but, what’s more efficient?

JSON vs Pickle

Python standard library includes JSON and Pickle mechanisms for serializing data. Some of things that we need to consider before of choice one of them are:

  • Performance in the serializing process
  • Memory consumption

To answer these questions I was created a small benchmarks that tests both methods. For do it I was created a test file that measures these two metrics by creating 10, 500 and 1000 random elements.

For these tests I also considered the UltraJson library. A high performance JSON library for Python.

I also tested the 2 latest version of Pickle: v4 and v5.

Results

Raw results of the analysis was the following:

Number of elements:  10
-----------------------

Json Size: 0.02715015411376953 MB
Json time: 0.006659951999999997 sec
UJson time: 0.0030970319999999996 sec
Pickle Size Proto 5: 0.02531909942626953 MB
Pickle time Proto 5: 0.003745575000000001 sec
Pickle Size Proto 4: 0.02531909942626953 MB
Pickle time Proto 4: 0.0031195769999999984 sec

Number of elements: 500
------------------------

Json Size: 24.98400592803955 MB
Json time: 1.651241939 sec
UJson time: 0.6243056419999997 sec
Pickle Size Proto 5: 25.035669326782227 MB
Pickle time Proto 5: 0.38723978699999995 sec
Pickle Size Proto 4: 25.035669326782227 MB
Pickle time Proto 4: 0.38475265300000006 sec

Number of elements: 1000
-------------------------

Json Size: 98.13797855377197 MB
Json time: 6.275222810999999 sec
UJson time: 2.5652533450000004 sec
Pickle Size Proto 5: 98.24791717529297 MB
Pickle time Proto 5: 1.3807646590000004 sec
Pickle Size Proto 4: 98.24791717529297 MB
Pickle time Proto 4: 1.3946891620000006 sec

Results as beautiful graphs:

Comparing memory usage

Size graph

We can saw that there’s not a real difference between JSON/UJson/Pickle v5/Pickle v4.

Comparing serializing time

Time graph

In performance is more obviously the difference. Pickle is the winner although UJson library is very close.

--

--

cr0hn

REST API Cybersecurity and Hacking & Python Architect. +100 GitHub projects. Speaker