<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
				<title>Comments on: serialize() vs. var_export() vs. json_encode()</title>
		<link>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html</link>
		<description>serialize() vs. var_export() vs. json_encode() Comment Feed</description>
				<language>de</language>
		<pubDate>Mon, 06 Sep 2010 07:49:53 +0000</pubDate>
		<generator>mcw[blog] 'Ruby' Alpha 3</generator>
		<item>
			<title>Comment #1 by: Séverin</title>
			<link>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7336</link>
			<guid>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7336</guid>
			<author>Séverin</author>			
			<pubDate>Mon, 16 Nov 2009 10:30:30 +0000</pubDate>
			<content:encoded><![CDATA[ Be carefull, your tests are not complete enough.<br />
I tested something else on my side : small arrays.<br />
<br />
# $array = array_fill(0, 20, rand(1, 9999));  <br />
# <br />
# $start = microtime(true);  <br />
# for ($i=0; $i<1000000; $i++) {<br />
#     $export = json_encode($array);<br />
# }<br />
# $end = microtime(true);  <br />
# $duration = $end - $start;  <br />
# print('JSON Encode: ' . $duration . PHP_EOL);  <br />
#   <br />
# $start = microtime(true);  <br />
# for ($i=0; $i<1000000; $i++) {<br />
#     $import = json_decode($export);<br />
# }<br />
# $end = microtime(true);  <br />
# $duration = $end - $start;  <br />
# print('JSON Decode: ' . $duration . PHP_EOL);<br />
<br />
The results are quite differents :<br />
<br />
* Serialize encode : 12.25s<br />
* Serialize decode : 6.64s<br />
* Total : 18.89s<br />
<br />
* Json encode : 4.85s<br />
* Json decode : 12.67s<br />
* Total : 17.52s<br />
<br />
I didn't try the var_export + eval case.<br />
<br />
With large arrays, I found the same results than you, but with small arrays, the difference isn't so big. And with a single entry array, serialize become faster.<br />
<br />
<br />
And, what happen with &#8230;<br />
<br />
> Large array containing objects : JSon is faster<br />
> Large array containing small arrays: JSon is faster+<br />
> Large array containing large arrays: JSon is faster++<br />
<br />
> Small array containing objects : Serialize is faster+<br />
> Small array containing small arrays : No differences<br />
> Small array containing large arrays : Serialize is faster++<br />
<br />
> String or Integer : Serialize is faster+++++++<br />
<br />
<br />
My conclusions :<br />
If you know the exact format of the data to store, use the best function (see above).<br />
If you are sure to store arrays but you don't know its size or content, use Json.<br />
If you have generic function to store datas that can be of any type including basic types such as string or integer, use serialize. ]]></content:encoded>
		</item>
		<item>
			<title>Comment #2 by: Dominik Jungowski</title>
			<link>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7339</link>
			<guid>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7339</guid>
			<author>Dominik Jungowski</author>			
			<pubDate>Mon, 16 Nov 2009 12:22:09 +0000</pubDate>
			<content:encoded><![CDATA[ That's quite interesting. Somehow I should have done that. If I have the time later I will benchmark all 3 possibilities with different array sizes and write a new blogpost about it ]]></content:encoded>
		</item>
		<item>
			<title>Comment #3 by: Jakob Ketterl</title>
			<link>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7368</link>
			<guid>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7368</guid>
			<author>Jakob Ketterl</author>			
			<pubDate>Tue, 17 Nov 2009 14:49:48 +0000</pubDate>
			<content:encoded><![CDATA[ there's another downside to the json approach: according to the documentation, the data that is to be encoded, needs to be in the utf-8 charset. i've tried ignoring that and ended up with corrupted data in my app. so if your data contains any special chars, you might stick with one of the slower method.<br />
<br />
p.s: yes one could use utf8_encode to fix that, but i do believe that that would make it too slow. i haven't benchmarked it though. ]]></content:encoded>
		</item>
		<item>
			<title>Comment #4 by: Séverin</title>
			<link>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7370</link>
			<guid>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7370</guid>
			<author>Séverin</author>			
			<pubDate>Tue, 17 Nov 2009 16:29:26 +0000</pubDate>
			<content:encoded><![CDATA[ And there's another thing to check : the memory used by each method. ]]></content:encoded>
		</item>
		<item>
			<title>Comment #5 by: EllisGL</title>
			<link>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7371</link>
			<guid>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7371</guid>
			<author>EllisGL</author>			
			<pubDate>Tue, 17 Nov 2009 16:49:01 +0000</pubDate>
			<content:encoded><![CDATA[ I rewrote a small caching class (JG_Cache) to use JSON, gave it a test.. Definately faster with serialize. ]]></content:encoded>
		</item>
		<item>
			<title>Comment #6 by: Dominik Jungowski</title>
			<link>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7373</link>
			<guid>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment7373</guid>
			<author>Dominik Jungowski</author>			
			<pubDate>Tue, 17 Nov 2009 17:05:09 +0000</pubDate>
			<content:encoded><![CDATA[ I just did a new benchmark with different array sizes and a fourth method - see latest blogpost.<br />
<br />
As for the memory usage: I have thought about that aswell, but I had no time  to benchmark that now. It would be nice to know nevertheless ]]></content:encoded>
		</item>
		<item>
			<title>Comment #7 by: DS</title>
			<link>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment15637</link>
			<guid>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html#comment15637</guid>
			<author>DS</author>			
			<pubDate>Fri, 09 Jul 2010 13:37:09 +0000</pubDate>
			<content:encoded><![CDATA[ Witam !<br />
Spróbuj takiego kodu : <br />
<?<br />
$str  = 'a:1:{i:0;a:12:{s:7:"id_kina";s:2:"20";s:9:"id_miasta";s:2:"22";s:14:"godziny_emisji";s:5:"20:15";s:12:"dzien_emisji";s:10:"2010-06-29";s:17:"ograniczenie_wiek";s:2:"15";s:6:"tytulp";s:7:"Fenomen";s:6:"tytulo";s:7:"Fenomen";s:6:"indeks";s:4:"1612";s:3:"rok";s:4:"2009";s:12:"nazwa_miasta";s:7:"Dębica";s:10:"nazwa_kina";s:9:"Śnieżka";s:10:"adres_kina";s:54:"Bojanowskiego 18, tel. (014) 676 08 48,(014) 670 31 67";}}';<br />
<br />
$arr = unserialize($str);<br />
$ile = 100000;<br />
<br />
$t1 = microtime(true);<br />
for($i = 0; $i<$ile; $i++) {<br />
	$xx = serialize($arr);<br />
	$yy = unserialize($xx);<br />
}<br />
$t2 = microtime(true);<br />
for($i = 0; $i<$ile; $i++) {<br />
    $xx = json_encode($arr);<br />
	$yy = json_decode($xx,true);<br />
}<br />
$t3 = microtime(true);<br />
<br />
$d1 = $t2 - $t1;<br />
$d2 = $t3 - $t2;<br />
<br />
<br />
echo "Czas nr1 : $d1, Czas nr2 : $d2"; <br />
?><br />
Na asocjasyjnych tablicach ze stringami ewidentnie szybciej działa serialize.<br />
Pozdrawiam ! ]]></content:encoded>
		</item>
		</channel>
</rss>