Preferred method to store PHP arrays (json_encode vs serialize)

前端 未结 20 1894
孤独总比滥情好
孤独总比滥情好 2020-11-22 05:55

I need to store a multi-dimensional associative array of data in a flat file for caching purposes. I might occasionally come across the need to convert it to JSON for use in

20条回答
  •  一向
    一向 (楼主)
    2020-11-22 06:35

    Y just tested serialized and json encode and decode, plus the size it will take the string stored.

    JSON encoded in 0.067085981369 seconds. Size (1277772)
    PHP serialized in 0.12110209465 seconds. Size (1955548)
    JSON decode in 0.22470498085 seconds
    PHP serialized in 0.211947917938 seconds
    json_encode() was roughly 80.52% faster than serialize()
    unserialize() was roughly 6.02% faster than json_decode()
    JSON string was roughly 53.04% smaller than Serialized string
    

    We can conclude that JSON encodes faster and results a smaller string, but unserialize is faster to decode the string.

提交回复
热议问题