Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelcolvin/11101522 to your computer and use it in GitHub Desktop.
Save samuelcolvin/11101522 to your computer and use it in GitHub Desktop.
Julia vs. Python JSON Performance comparison
versioninfo()
using JSON
tic()
fio = open("test_data.json", "r")
text = readall(fio)
print("file loaded and read: ")
toc()
print("Parsing JSON from string: ")
@time jjson_data = JSON.parse(text)
fio = open("test_data.json", "r")
print("Parsing JSON directly from file: ")
@time jjson_data = JSON.parse(fio)
sio = IOBuffer()
print("Generating JSON: ")
@time JSON.print(sio, jjson_data)
from random import random
from time import time
import sys, json, ujson
print sys.version
data = []
print 'generating data...'
no_strings = True
for i in range(int(1e4)):
if random() > 0.5:
if no_strings:
data.append([v*random() for v in range(int(random()*500))])
else:
data.append({str(random()): v*random() for v in range(int(random()*500))})
else:
data.append(list(range(int(random()*500))))
print 'performing tests...'
t1 = time()
json_str = json.dumps(data, separators=(',',':'))
t2 = time()
print('python json dumps time: %0.3fs, len = %0.2fm' % (t2 - t1, len(json_str)/1e6))
ujson_str = ujson.dumps(data)
t3 = time()
print('python ujson dumps time: %0.3fs, len = %0.2fm' % (t3 - t2, len(ujson_str)/1e6))
json_data2 = json.loads(json_str)
t4 = time()
print('python json loads time: %0.3fs' % (t4 - t3))
ujson_data2 = ujson.loads(json_str)
t5 = time()
print('python ujson loads time: %0.3fs' % (t5 - t4))
json.dump(data, open('test_data.json', 'w'), separators=(',',':'))

see JuliaIO/JSON.jl#59 for discussion.

Python results (with strings in JSON):

generating data...
performing tests...
python json dump time:    4.063s, len = 48.11m
python ujson dump time:   1.465s, len = 42.74m
python json load time:    3.004s
python ujson load time:   1.012s

This generates a json file 40-50mb in size.

Julia results (with strings in JSON):

file loaded and read:            elapsed time: 1.177662644 seconds
Parsing JSON from string:        elapsed time: 25.780348228 seconds (831651540 bytes allocated)
Parsing JSON directly from file: elapsed time: 61.440253927 seconds (1012071980 bytes allocated)
Generating JSON:                 elapsed time: 7.397358822 seconds (211069604 bytes allocated)

Python results (without strings in JSON):

python json dump time:    3.294s, len = 27.51m
python ujson dump time:   1.086s, len = 22.09m
python json load time:    1.634s
python ujson load time:   0.472s

Julia results (without strings in JSON):

file loaded and read:            elapsed time: 0.484029716 seconds
Parsing JSON from string:        elapsed time: 6.738140036 seconds (441511036 bytes allocated)
Parsing JSON directly from file: elapsed time: 14.05050392 seconds (534468676 bytes allocated)
Generating JSON:                 elapsed time: 3.023333918 seconds (130109004 bytes allocated)

Version Info:

Python:
    2.7.5+ (default, Feb 27 2014, 19:39:55) 
    [GCC 4.8.1]

Julia:
    Julia Version 0.3.0-prerelease+2682
    Commit 5291e96* (2014-04-19 20:22 UTC)
    Platform Info:
      System: Linux (i686-linux-gnu)
      CPU: Intel(R) Xeon(R) CPU           E5320  @ 1.86GHz
      WORD_SIZE: 32
      BLAS: libopenblas (DYNAMIC_ARCH NO_AFFINITY)
      LAPACK: libopenblas
      LIBM: libopenlibm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment