*fast* serialize/unserialize?

I have a PHP script that builds a binary search tree over a rather large CSV file (5MB+). This is nice and all, but it takes about 3 seconds to read/parse/index the file.

Now I thought I could use serialize() and unserialize() to quicken the process. When the CSV file has not changed in the meantime, there is no point in parsing it again.

To my horror I find that calling serialize() on my index object takes 5 seconds and produces a huge (19MB) text file, whereas unserialize() takes unbearable 27 seconds to read it back. Improvements look a bit different. ;-)

So - is there a faster mechanism to store/restore large object graphs to/from disk in PHP?

(To clarify: I'm looking for something that takes significantly less than the aforementioned 3 seconds to do the de-serialization job.)


It seems that the answer to your question is no.

Even if you discover a "binary serialization format" option most likely even that would be to slow for what you envisage.

So, what you may have to look into using (as others have mentioned) is a database, memcached, or on online web service.

I'd like to add the following ideas as well:

  • caching of requests/responses
  • your PHP script does not shutdown but becomes a network server to answer queries
  • or, dare I say it, change the data structure and method of query you are currently using

  • var_export should be lots faster as PHP won't have to process the string at all:

    // export the process CSV to export.php
    $php_array = read_parse_and_index_csv($csv); // takes 3 seconds
    $export = var_export($php_array, true);
    file_put_contents('export.php', '<?php $php_array = ' . $export . '; ?>');
    

    Then include export.php when you need it:

    include 'export.php';
    

    Depending on your web server set up, you may have to chmod export.php to make it executable first.


    Try igbinary...did wonders for me:

    http://pecl.php.net/package/igbinary

    链接地址: http://www.djcxy.com/p/57666.html

    上一篇: 解析速度更快? 序列化的字符串或普通的PHP或其他东西?

    下一篇: *快速*序列化/反序列化?