Closed mamrehn closed 5 years ago
Thanks for the report, it seems that pycson incorrectly disallows upper-case E
in numbers with exponents.
By the way, you'll probably be very disappointed by the speed. Could you post the numbers here though? I'm interested.
Thank you for the fast response!
Ah, so the E-8
is probably part of 1.2345E-8
or similar. Is this a bug in speg
then?
Regarding the benchmark, here we go:
import timeit
from urllib.request import urlopen
def test(name, url):
json_content = urlopen(url).read()
num_evals = 512
globals_ = {'json_str': json_content}
tu = timeit.timeit('loads(json_str)', number=num_evals, globals=globals_,
setup='from ujson import loads') # pip install ujson
tj = timeit.timeit('loads(json_str)', number=num_evals, globals=globals_,
setup='from json import loads')
tc = timeit.timeit('loads(json_str)', number=num_evals, globals=globals_,
setup='from cson import loads') # pip install cson
print(f'Testing "{name}" JSON file with {len(json_content)} characters')
print('Timings: '
f'{tu / num_evals:0.5f}s ujson parser, ' +
f'{tj / num_evals:0.5f}s json parser, ' +
f'{tc / num_evals:0.5f}s cson parser')
print(f'ujson parser is {tj / tu:0.3f}x faster than json parser')
print(f'ujson parser is {tc / tu:0.3f}x faster than cson parser')
print(f'json parser is {tc / tj:0.3f}x faster than cson parser')
if __name__ == '__main__':
data = {
'small': 'https://next.json-generator.com/api/json/get/E1WxTbxmL',
'large': 'https://next.json-generator.com/api/json/get/EkWD0-lX8',
}
for name_, url_ in data.items():
test(name_, url_)
print()
Testing "small" JSON file with 11030 characters
Timings: 0.00010s ujson parser, 0.00006s json parser, 0.06606s cson parser
ujson parser is 0.604x faster than json parser
ujson parser is 679.771x faster than cson parser
json parser is 1124.894x faster than cson parser
Testing "large" JSON file with 38218 characters
Timings: 0.00024s ujson parser, 0.00032s json parser, 0.42206s cson parser
ujson parser is 1.324x faster than json parser
ujson parser is 1739.524x faster than cson parser
json parser is 1313.395x faster than cson parser
I was not looking for a faster version of the built-in json parser (for this I would use ujson). I was just curious whether the parsing speed is comparable to the standard library parser (which it is not, as you also mentioned).
So for JSON files larger than 100KB I would not use cson to parse then.
This is now fixed in cson-0.8, thanks again for the report and thank you for the numbers!
In the README there is the statement
So I assume, one could parse any JSON file with cson. For this, I tried to benchmark the parsing time via
and got
Is there an intuition what might be wrong here, or is a minimal working example including a JSON file required?