Unserialize vs Include

Posted by John Kleijn • Sunday, December 7. 2008 • Category: PHP

Cutting straight to chase, the results:

r448191@goliath:~/bench$ php bench.php 10

Total include: 0.000389575958252

Total unserialize: 0.000274181365967

r448191@goliath:~/bench$ php bench.php 100

Total include: 0.0033860206604

Total unserialize: 0.00247645378113

r448191@goliath:~/bench$ php bench.php 1000

Total include: 0.0342044830322

Total unserialize: 0.0263719558716

r448191@goliath:~/bench$ php bench.php 10000

Total include: 0.32866358757

Total unserialize: 0.25150680542

r448191@goliath:~/bench$ php bench.php 100000

Total include: 3.23683238029

Total unserialize: 2.56990027428


The data (data.php):


<?php
return array(
        'foo' => 'bar',
        'meh' => array(
                'o' => 'my',
                'god' => 'this',
                'is' => 'very'
        ),
        'tedious'      
);
 

The test script (run from CLI with iteration argument):


<?php
class Stopwatch
{
        private $_start;
       
        private $_stop;
       
        private $_elapsed = 0;
       
        public function start()
        {
                $this->_start = microtime(true);       
        }
       
        public function stop()
        {
                $this->_stop = microtime(true);
               
                $this->_elapsed += $this->_stop - $this->_start;
        }
       
        public function getElapsed()
        {
                return $this->_elapsed;
        }
}
class QuickNDirtyBench
{
        private $_serializationFile;
       
        public function __construct($serializationFile)
        {
                $this->_serializationFile = $serializationFile;
        }
       
        public function benchUnserialize($i)
        {
                $stopwatch = new Stopwatch();
                       
                for (; $i > 0 ; --$i)
                {
                        $stopwatch->start();
                       
                        $array = unserialize(file_get_contents($this->_serializationFile));
                       
                        $stopwatch->stop();
                }
               
                return $stopwatch->getElapsed();
        }
       
        public function benchInclude($i)
        {
                $stopwatch = new Stopwatch();
                 
                for (; $i > 0 ; --$i)
                {
                        $stopwatch->start();
                       
                        $array = include 'data.php';
                       
                        $stopwatch->stop();
                }
               
                file_put_contents($this->_serializationFile, serialize($array));
               
                return $stopwatch->getElapsed();
        }
       
        public function sanityCheck()
        {
                $array1 = include 'data.php';
               
                if(!is_array($array1))
                {
                        throw new Exception("Sanity check failed");
                }
               
                $array2 = unserialize(file_get_contents($this->_serializationFile));
               
                if(!is_array($array1))
                {
                        throw new Exception("Sanity check failed");
                }              
               
                if($array1 !== $array2)
                {
                        throw new Exception("Sanity check failed");
                }
        }
}

$bencher = new QuickNDirtyBench('serialized.txt');

echo "Total include: {$bencher->benchInclude($argv[1])}\\n";
echo "Total unserialize: {$bencher->benchUnserialize($argv[1])}\\n";

$bencher->sanityCheck();
 

0 Trackbacks

  1. No Trackbacks

0 Comments

Display comments as (Linear | Threaded)
  1. No comments

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA


Antiquities and such