PHP - *fast* serialize/unserialize?

前端 未结 8 1753
死守一世寂寞
死守一世寂寞 2021-02-19 04:54

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.

<
8条回答
  •  庸人自扰
    2021-02-19 05:23

    i see two options here

    string serialization, in the simplest form something like

      write => implode("\x01", (array) $node);
      read  => explode() + $node->payload = $a[0]; $node->value = $a[1] etc
    

    binary serialization with pack()

      write => pack("fnna*", $node->value, $node->le, $node->ri, $node->payload);
      read  => $node = (object) unpack("fvalue/nre/nli/a*payload", $data);
    

    It would be interesting to benchmark both options and compare the results.

提交回复
热议问题