views:

341

answers:

4

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  A: 

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.

Paolo Bergantino
+3  A: 

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.

Chad Birch
Thanks, this seems to be the easiest, didn't know PHP's own serialization was this simple.
The Unknown
A: 

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.

karim79
This is also one of the few circumstances where using eval() is advisable. SOLR's "php" return type returns a similarly formatted string, which you're supposed to eval()
Frank Farmer
eval() can be used, and does the job, but why is it advisable to use it over the technique above (not to be argumentative :) ? I used the above method for processing huge numbers of very large text files.
karim79
A: 

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++:

Robert S. Barnes