#6055: serialize/unserialize vs json_encode/json_decode
---------------------------+------------------------------------------------
Reporter: aramisbear | Type: Optimization
Status: new | Priority: Medium
Milestone: 1.2.x.x | Component: Core Libs
Version: 1.2 Final | Severity: Normal
Keywords: | Php_version: n/a
Cake_version: |
---------------------------+------------------------------------------------
After looking through the caching system and persistent file structure I
saw the serialize and unserialize functions being used over and over.
This got me wondering, if Cake is unserializing all of that information
every time the page loads, how much overhead is that? I noticed the same
structure in the cache files that are generated by the system. I decided
to compare the results of serialize/unserialize against
json_encode/json_decode to see what happened and believe that this might
provide an opportunity to performance tune Cake.
This was a fairly simple test using the microtime function. I generated a
large array structure and then serialized it 100 times, json_encoded it
100 times, unserialized it 100 times, and json_decoded it 100 times. Here
are the final results:
{{{
MEMORY LIMIT: 128M
NEW MEMORY LIMIT: 512M
Generating test array structure
Test array generated
Average serialize time: 0.41118302822113 (ms)
Average json_encode time: 0.63083570957184 (ms)
Average unserialize time: 2.2801760816574 (ms)
Average json_decode time: 1.4209048509598 (ms)
}}}
Given that unserialize is used tremendously more often than serialize, the
results make me wonder if swapping out those functions for the json
functions would provide a performance boost to CakePHP. Are there any
benefits to using unserialize over the json functions? Could it use the
json_methods if they were available?
Here is the code used in my test:
{{{
<?php
echo "\nMEMORY LIMIT: " . ini_get('memory_limit');
ini_set('memory_limit','512M');
echo "\nNEW MEMORY LIMIT: " . ini_get('memory_limit');
$start = microtime();
echo "\nGenerating test array structure";
$test = array();
for($i = 0; $i < 500; $i++) {
$test[md5($i)] = rand();
}
$bigger = array();
for($i = 0; $i < 1000; $i++) {
$bigger[md5(rand())] = $test;
}
echo "\nTest array generated";
$times = array();
for($i = 0; $i < 100; $i++) {
$start_s = microtime(true);
serialize($bigger);
$end_s = microtime(true);
$times[] = $end_s - $start_s;
if($end_s < $start_s) echo "\nEnd: $end_s Start: $start_s";
}
$average = array_sum($times) / count($times);
echo "\nAverage serialize time: " . $average . ' (ms)';
$times = array();
for($i = 0; $i < 100; $i++) {
$start_s = microtime(true);
json_encode($bigger);
$end_s = microtime(true);
$times[] = $end_s - $start_s;
if($end_s < $start_s) echo "\nEnd: $end_s Start: $start_s";
}
$average = array_sum($times) / count($times);
echo "\nAverage json_encode time: " . $average . ' (ms)';
$times = array();
$serialized = serialize($bigger);
for($i = 0; $i < 100; $i++) {
$start_s = microtime(true);
$stored = unserialize($serialized);
$end_s = microtime(true);
$times[] = $end_s - $start_s;
if($end_s < $start_s) echo "\nEnd: $end_s Start: $start_s";
}
$average = array_sum($times) / count($times);
echo "\nAverage unserialize time: " . $average . ' (ms)';
$times = array();
$jsoned = json_encode($bigger);
for($i = 0; $i < 100; $i++) {
$start_s = microtime(true);
$stored = json_decode($jsoned);
$end_s = microtime(true);
$times[] = $end_s - $start_s;
if($end_s < $start_s) echo "\nEnd: $end_s Start: $start_s";
}
$average = array_sum($times) / count($times);
echo "\nAverage json_decode time: " . $average . ' (ms)';
echo "\n";
?>
}}}
--
Ticket URL: <https://trac.cakephp.org/ticket/6055>
CakePHP : The Rapid Development Framework for PHP <https://trac.cakephp.org/>
Cake is a rapid development framework for PHP which uses commonly known design
patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC.
Our primary goal is to provide a structured framework that enables PHP users at
all levels to rapidly develop robust web applications, without any loss to
flexibility.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"tickets cakephp" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/tickets-cakephp?hl=en
-~----------~----~----~----~------~----~------~--~---