Python 2.6 JSON decoding performance

后端 未结 7 1307
春和景丽
春和景丽 2020-11-30 05:30

I\'m using the json module in Python 2.6 to load and decode JSON files. However I\'m currently getting slower than expected performance. I\'m using a test case

相关标签:
7条回答
  • 2020-11-30 06:15

    For those who are parsing output from a request using the requests package, e.g.:

    res = requests.request(...)
    
    text = json.loads(res.text)
    

    This can be very slow for larger response contents, say ~45 seconds for 6 MB on my 2017 MacBook. It is not caused by a slow json parser, but instead by a slow character set determination by the res.text call.

    You can solve this by setting the character set before you are calling res.text, and using the cchardet package (see also here):

    if res.encoding is None:
        res.encoding = cchardet.detect(res.content)['encoding']
    

    This makes the response text json parsing almost instant!

    0 讨论(0)
提交回复
热议问题