Fastest serialize data format form PHP reading

不问归期 提交于 2019-12-23 16:32:50

问题


I have a PHP frontend and a C++ backend, and I need to be able to send groups of names to the frontend. What serialized format would be the most efficient/fastest for the PHP to read?

Example data

group1:
  name1 3923
  name2 9879
  name3 8944
group2:
  name5 9823
group3: 
  name9 9822
  name1 4894

What would be the fastest for PHP to read?

  • XML
  • JSON
  • YAML
  • Protocol Buffer
  • Comma/Space Delimited our own system
  • Anything else? other?

回答1:


PHP's own serialized format will probably be the fastest. unserialize() is the function PHP uses to convert this data back to its own types. This post has various links to other languages' implementations of PHP's serialized format, I'm sure you could convert one of those easily.




回答2:


I've used PHP's serialize() and unserialize() on large text files, and it performed miserably (that was a couple of years ago - maybe it's better now). Anyway, I devised a little trick to overcome this, it simply involves generating a PHP array declaration from the data you're exporting straight into a text file, e.g.:

<?php
$groups = array('groups' => array( array('jeff' => 2343,
                                         'tom'  => 8477),
                                   array('baal' => 2873,
                                         'scorpio'  => 3210),
                                   array('jeff' => 2343,
                                         'tom'  => 8477)
                                 )
                            )
               );
?>

...and then unserializing it by simply calling:

include 'groups.php';//makes $groups available

Worked nicely back then.




回答3:


JSON would be pretty easy using json_decode. I'm not sure about speed, but unless you plan on transferring megabytes of this data between the systems it should be irrelevant which one you go with.




回答4:


As Paolo pointed out you can use json_decode which is very fast. On the C++ backend these are some of your options ( taken directly from the json.org website ):

C++:

  • TinyJSON.
  • jsoncpp.
  • zoolib.
  • Jaula.
  • JOST.
  • JSON Spirit.
  • CAJUN.


来源:https://stackoverflow.com/questions/866629/fastest-serialize-data-format-form-php-reading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!