Preferred method to store PHP arrays (json_encode vs serialize)

前端 未结 20 2154
孤独总比滥情好
孤独总比滥情好 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:36

    I augmented the test to include unserialization performance. Here are the numbers I got.

    Serialize
    
    JSON encoded in 2.5738489627838 seconds
    PHP serialized in 5.2861361503601 seconds
    Serialize: json_encode() was roughly 105.38% faster than serialize()
    
    
    Unserialize
    
    JSON decode in 10.915472984314 seconds
    PHP unserialized in 7.6223039627075 seconds
    Unserialize: unserialize() was roughly 43.20% faster than json_decode() 
    

    So json seems to be faster for encoding but slow in decoding. So it could depend upon your application and what you expect to do the most.

提交回复
热议问题