serialize() vs. var_export() vs. json_encode() Memory Usage

Permanent Link: serialize() vs. var_export() vs. json_encode() Memory Usage 18. November 2009 Comment Comments (4)

Based on serialize() vs. var_export() vs. json_encode() Part 2, let's take a look at the (real) memory usage:

Apart from var_export allo methods seem to use about the same. Let's put the usage in relation to each other to get a better view:

With smaller arrays, the memory usage is exactly the same, from 10.000 elements on the values begin to diverge.

serialize() vs. var_export() vs. json_encode() Part 2

Permanent Link: serialize() vs. var_export() vs. json_encode() Part 2 17. November 2009 Comment Comments (2)

Based on the comments from my first benchmark in the serialize() vs. var_export() vs. json_encode article 2 days ago, I decided to benchmark once again, this time using different array sizes and I also added the JSON method with recursive UTF8 encoding beforehand. This time, all results are the combined results (exporting and importing), the testing script was exactly the same (except JSON+UTF8 which also had a utf8_encode_recursive function). Let's see what happens when using array sizes from 10 to 1000 elements:

So far, json_encode itself is fastest, closely followed by serialization. JSON with UTF8 Encoding is slowest, but consider that all the values are all below 0,02 seconds.

So far so good, let's look at the results with arrays from 10.000 to 1.000.000 elements:

Apart from the exception Serialization the values mostly develop like with smaller arrays. What's quite odd is that Serialization's runtime seems to be exploding when stepping up from 100.000 to 1.000.000. Why that is so, I cannot tell. Although JSON with UTF8 Encoding is slower than var_export and JSON itself, it's still way faster than serialization.

Conclusions

  1. None of the methods scales linear.
  2. For smaller arrays, JSON is the way to go, as long as your data is already UTF8 encoded. If not, you might want to take serialization.
  3. With larger arrays JSON is still fastest as long as your data is already UTF8 encoded, otherwise var_export is the best choice.

Closing, let's take a look at the graphs from 10 - 1.000.000 item large arrays:

serialize() vs. var_export() vs. json_encode()

Permanent Link: serialize() vs. var_export() vs. json_encode() 16. November 2009 Comment Comments (7)

There are times when you need to store an array, for example when your array is an index you wanna use again the next time you run your application. In order to store an array you have to transform it into some kind of string represantation first, most people would probably use serialize(). But there are also 2 other ways to achieve that: var_export() and json_encode().

After having them stored the functions to interpret the strings as arrays would be unserialize() if you use serialize(), eval() if you use var_export() and json_decode() if you use json_encode().

So, what about the performance?

In order to test that I wrote a little profiling script that first created some random array with 1.000.000 elements, then exported the array and then imported it again. For the json_encode() test the script looked like that:

$array = array_fill(0, 1000000, rand(1, 9999));

$start = microtime(true);
$export = json_encode($array);
$end = microtime(true);
$duration = $end - $start;
print('JSON Encode: ' . $duration . PHP_EOL);

$start = microtime(true);
$import = json_decode($export);
$end = microtime(true);
$duration = $end - $start;
print('JSON Decode: ' . $duration . PHP_EOL);

Apart from the exporting and importing functions used, the script for serialize() and var_export() looked pretty much the sime, var_export() being the only exception, since I had to add an ending ; to $export in order for it to work with eval().

While it is understandable that importing takes longer for every method than importing, the differences in time are quite astounding:

It's not only interesting to see that unserialize() is damn slow but also that JSON is fastest, which also gets quite clear when looking at combined results:

 

Since it's still in the Ubuntu repositories, I did the performance tests with PHP 5.2.6

Speaking at PHP Conference 2009 about Scrum

Permanent Link: Speaking at PHP Conference 2009 about Scrum 15. November 2009 Comment No Comment

Today (November, 15th) I'm giving a workshop about Scrum basics at the PHP Conference 2009 in Karlsruhe, Germany together with my colleague Sebastian Schürmann. If you're new to scrum and/or have never heard about it, it should be a quite interesting workshop for you.

Article "Mobile Webapplications" in german PHP Magazin

Permanent Link: Article 11. November 2009 Comment No Comment

As of today issue 1.2010 of the german "PHP Magazin" is available, containing my article "Mobile Webapplications" with tips & tricks for developing your own Mobile Webapplication using PHP.

As for the "OOP in JavaScript" series: I didn't forget about it, I just didn't have enough time to write the fourth part of it which will be covering scoping.

< Oktober 2009 | Dezember 2009 >