Preferred method to store PHP arrays (json_encode vs serialize)

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

    I know this is late but the answers are pretty old, I thought my benchmarks might help as I have just tested in PHP 7.4

    Serialize/Unserialize is much faster than JSON, takes less memory and space, and wins outright in PHP 7.4 but I am not sure my test is the most efficient or the best,

    I have basically created a PHP file which returns an array which I encoded, serialised, then decoded and unserialised.

    $array = include __DIR__.'/../tests/data/dao/testfiles/testArray.php';
    
    //JSON ENCODE
    $json_encode_memory_start = memory_get_usage();
    $json_encode_time_start = microtime(true);
    
    for ($i=0; $i < 20000; $i++) { 
        $encoded = json_encode($array);
    }
    
    $json_encode_time_end = microtime(true);
    $json_encode_memory_end = memory_get_usage();
    $json_encode_time = $json_encode_time_end - $json_encode_time_start;
    $json_encode_memory = 
    $json_encode_memory_end - $json_encode_memory_start;
    
    
    //SERIALIZE
    $serialize_memory_start = memory_get_usage();
    $serialize_time_start = microtime(true);
    
    for ($i=0; $i < 20000; $i++) { 
        $serialized = serialize($array);
    }
    
    $serialize_time_end = microtime(true);
    $serialize_memory_end = memory_get_usage();
    $serialize_time = $serialize_time_end - $serialize_time_start;
    $serialize_memory = $serialize_memory_end - $serialize_memory_start;
    
    
    //Write to file time:
    $fpc_memory_start = memory_get_usage();
    $fpc_time_start = microtime(true);
    
    for ($i=0; $i < 20000; $i++) { 
        $fpc_bytes = 
        file_put_contents(
            __DIR__.'/../tests/data/dao/testOneBigFile',
            ';'
        );
    }
    
    $fpc_time_end = microtime(true);
    $fpc_memory_end = memory_get_usage();
    $fpc_time = $fpc_time_end - $fpc_time_start;
    $fpc_memory = $fpc_memory_end - $fpc_memory_start;
    
    
    //JSON DECODE
    $json_decode_memory_start = memory_get_usage();
    $json_decode_time_start = microtime(true);
    
    for ($i=0; $i < 20000; $i++) { 
        $decoded = json_encode($encoded);
    }
    
    $json_decode_time_end = microtime(true);
    $json_decode_memory_end = memory_get_usage();
    $json_decode_time = $json_decode_time_end - $json_decode_time_start;
    $json_decode_memory = 
    $json_decode_memory_end - $json_decode_memory_start;
    
    
    //UNSERIALIZE
    $unserialize_memory_start = memory_get_usage();
    $unserialize_time_start = microtime(true);
    
    for ($i=0; $i < 20000; $i++) { 
        $unserialized = unserialize($serialized);
    }
    
    $unserialize_time_end = microtime(true);
    $unserialize_memory_end = memory_get_usage();
    $unserialize_time = $unserialize_time_end - $unserialize_time_start;
    $unserialize_memory = 
    $unserialize_memory_end - $unserialize_memory_start;
    
    
    //GET FROM VAR EXPORT:
    $var_export_memory_start = memory_get_usage();
    $var_export_time_start = microtime(true);
    
    for ($i=0; $i < 20000; $i++) { 
        $array = include __DIR__.'/../tests/data/dao/testOneBigFile';
    }
    
    $var_export_time_end = microtime(true);
    $var_export_memory_end = memory_get_usage();
    $var_export_time = $var_export_time_end - $var_export_time_start;
    $var_export_memory = $var_export_memory_end - $var_export_memory_start;
    

    Results:

    Var Export length: 11447 Serialized length: 11541 Json encoded length: 11895 file put contents Bytes: 11464

    Json Encode Time: 1.9197590351105 Serialize Time: 0.160325050354 FPC Time: 6.2793469429016

    Json Encode Memory: 12288 Serialize Memory: 12288 FPC Memory: 0

    JSON Decoded time: 1.7493588924408 UnSerialize Time: 0.19309520721436 Var Export and Include: 3.1974139213562

    JSON Decoded memory: 16384 UnSerialize Memory: 14360 Var Export and Include: 192

提交回复
热议问题