Performance of the unserialize function

    PHP has two great functions, serialize and unserialize. The first converts practically any data set into a string, the second performs the inverse transformation. These functions are convenient to use when organizing caching or storing sessions in a database. I found that the unserialize function may run unexpectedly long.

    I will not describe how and why I needed to serialize and deserialize a large array of data, it is much more interesting to look at what I found.
    1. ini_set('memory_limit', '512M');
    2. $file = '/tmp/1';
    3. $data = range(1,2000000);
    4.  
    5. echo "Test serialize\n";
    6. $time0 = microtime(1);
    7. file_put_contents($file, serialize($data));
    8. $time1 =  microtime(1);
    9. unserialize(file_get_contents($file));
    10. $time2 =  microtime(1);
    11.  
    12. $timeset = $time1-$time0;
    13. $timeget = $time2-$time1;
    14.  
    15. echo "Serialize set time $timeset get time $timeget\n";

    Test serialize
    Serialize set time 1.35619807243 get time 31.1126699448
    


    30 seconds to deserialize! This result simply shocked me. For starters, I checked that file_get_contents does not affect the execution result. Then I looked at the performance of json_encode and json_decode (JSON set time 0.270335912704 get time 1.30652809143). “Everything is weirder and weirder,” I thought, and decided to build a graph of the dependence of the operating time of the unserialize function on the length of the deserialized array. The graph clearly shows a quadratic (!) Dependence of the execution time of the function on the length of the array. Here is such an unexpectedly slow built-in unserialize function. The situation, of course, is not critical. You can use other methods of serialization and deserialization. The main goal of the article is to show how the performance of the unserialize function depends on the size of the data.







    Source codes for checking the results can be found at  http://alexxz.ru/habr/unserialize_benchmark.tar.gz

    Used software and hardware PHP 5.3.2
    Linux ubuntu 2.6.32-24-generic (10.4)
    Intel ® Core 2 CPU 6600 @ 2.40GHz

    ______________________
    The text is prepared in the Blog Editor by © SoftCoder.ru

    Also popular now: