Wednesday, May 26, 2010

hız önemli mîrim

json_encode ve serialize arasındaki hız farkını bir de ben test etmek istedim hepsi bu.
Bendeki sonuç şuydu
JSON encoded in 0.17906808853149 seconds
PHP serialized in 0.4260368347168 seconds
json_encode() was roughly 137.92% faster than serialize()


Konuyu irdeleyin :
http://www.google.com.tr/search?sourceid=chrome&ie=UTF-8&q=json_encode+vs+serialize

<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$testArray = fillArray( 0, 4 );
//print_r($testArray );    

$start = microtime( true );
json_encode( $testArray );
$jsonTime = microtime( true ) - $start;
echo "JSON encoded in $jsonTime seconds<br>";

$start = microtime( true );
serialize( $testArray );
$serializeTime = microtime( true ) - $start;
echo "PHP serialized in $serializeTime seconds<br>";

if ( $jsonTime < $serializeTime )
    echo "json_encode() was roughly " . number_format( ($serializeTime / $jsonTime - 1 ) * 100, 2 ) . "% faster than serialize()";
else if ( $serializeTime < $jsonTime )
    echo "serialize() was roughly " . number_format( ($jsonTime / $serializeTime - 1 ) * 100, 2 ) . "% faster than json_encode()";
else 
    echo 'Unpossible!';


function fillArray( $depth, $max )
{
    static $seed;
    if ( is_null( $seed ) )
        $seed = array( 1,'a',2,'b',3,'c',4,'d',5,'e',6,'f',7,'g',8,'h',9,'i',0,'j' );
    
    if ( $depth < $max )
    {
        $node = array();
        foreach ( $seed as $key )
         $node[$key] = fillArray( $depth + 1, $max );
       
        return $node;
    }
    return 'empty';
}
?>