Skip to content

Instantly share code, notes, and snippets.

@ramunasd
Last active December 14, 2015 08:09
Show Gist options
  • Save ramunasd/5056114 to your computer and use it in GitHub Desktop.
Save ramunasd/5056114 to your computer and use it in GitHub Desktop.
Benchmark most popular PHP serializers.
<?php
$_testStrings = array(
'AK' => 'Alaska', 'AZ' => 'Arizona', 'VT' => 'Vermont',
'VA' => 'Virginia', 'AZ' => 'West Virginia',
);
$_testIntegers = array(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 84, 144,);
$_testBooleans = array(TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE,);
$_testFloats = array(
0, 1.1, 1.1, 2.22, 3.33, 5.55, 8.88, 13.13, 21.2121, 34.3434,
55.5555, 84.8484, 144.144,
);
$_testMixed = array(
'one', 13 => 'two', 0 => 25.46, 'four' => 0.007,
'five' => TRUE, TRUE => 42,
);
$_objectOne = new stdClass();
$_objectOne->firstname = 'Leroy';
$_objectOne->lastname = 'Jenkins';
$_objectOne->profession = 'Gamer';
$_objectOne->status = 'Legend';
$_objectTwo = new stdClass();
$_objectTwo->series = 'Fibonacci';
$_objectTwo->data = $_testIntegers;
$_testObjects = array($_objectOne, $_objectTwo,);
$_maxLoop = 1000000;
$_templateEncode = '%s [%s]: Size: %s bytes, %s time to encode' . PHP_EOL;
$_templateDecode = '%s [%s]: %s time to decode' . PHP_EOL;
set_time_limit(0);
$_output = '';
/**
* Set the source arrays
*/
$_allTestData = array(
'str' => $_testStrings,
'int' => $_testIntegers,
'bln' => $_testBooleans,
'flt' => $_testFloats,
'mix' => $_testMixed,
'obj' => $_testObjects,
);
$_testSources = array(
'strings' => $_testStrings,
'integers' => $_testIntegers,
'booleans' => $_testBooleans,
'floats' => $_testFloats,
'mixed' => $_testMixed,
'objects' => $_testObjects,
'all' => $_allTestData,
);
/**
* ENCODE DATA
*/
/**
* Start each test
*/
foreach ($_testSources as $_area => $_source)
{
/**
* Start the timer
*/
$_serializeStart = microtime(true);
for ($_counter = 0; $_counter < $_maxLoop; $_counter++)
{
serialize($_source);
}
$_serializeEnd = microtime(true);
$_serializeOutput = serialize($_source);
$_output .= sprintf(
$_templateEncode,
'serialize()',
$_area,
strlen($_serializeOutput),
$_serializeEnd - $_serializeStart
);
/**
* JSON
*/
$_jsonStart = microtime(TRUE);
for ($_counter = 0; $_counter < $_maxLoop; $_counter++)
{
json_encode($_source);
}
$_jsonEnd = microtime(TRUE);
$_jsonOutput = json_encode($_source);
$_output .= sprintf(
$_templateEncode,
'json_encode()',
$_area,
strlen($_jsonOutput),
$_jsonEnd - $_jsonStart
);
/**
* igbinary
*/
$_igbinaryStart = microtime(TRUE);
for ($_counter = 0; $_counter < $_maxLoop; $_counter++)
{
igbinary_serialize($_source);
}
$_igbinaryEnd = microtime(TRUE);
$_igbinaryOutput = igbinary_serialize($_source);
$_output .= sprintf(
$_templateEncode,
'igbinary_serialize()',
$_area,
strlen($_igbinaryOutput),
$_igbinaryEnd - $_igbinaryStart
);
$_output .= str_repeat('=', 20) . "\r\n";
}
$_output .= str_repeat('=:=', 20) . "\r\n";
/**
* DECODE DATA
*/
/**
* Start each test
*/
foreach ($_testSources as $_area => $_source)
{
/**
* Start the timer
*/
$_data = serialize($_source);
$_serializeStart = microtime(TRUE);
for ($_counter = 0; $_counter < $_maxLoop; $_counter++)
{
unserialize($_data);
}
$_serializeEnd = microtime(TRUE);
$_output .= sprintf(
$_templateDecode,
'unserialize()',
$_area,
$_serializeEnd - $_serializeStart
);
/**
* JSON
*/
$_data = json_encode($_source);
$_jsonStart = microtime(TRUE);
for ($_counter = 0; $_counter < $_maxLoop; $_counter++)
{
json_decode($_data, TRUE);
}
$_jsonEnd = microtime(TRUE);
$_jsonOutput = json_encode($_source);
$_output .= sprintf(
$_templateDecode,
'json_decode()',
$_area,
$_jsonEnd - $_jsonStart
);
/**
* igbinary
*/
$_data = igbinary_serialize($_source);
$_igbinaryStart = microtime(TRUE);
for ($_counter = 0; $_counter < $_maxLoop; $_counter++)
{
igbinary_unserialize($_data);
}
$_igbinaryEnd = microtime(TRUE);
$_igbinaryOutput = igbinary_serialize($_source);
$_output .= sprintf(
$_templateDecode,
'igbinary_unserialize()',
$_area,
$_igbinaryEnd - $_igbinaryStart
);
$_output .= str_repeat('=', 20) . "\r\n";
}
echo '<pre>', $_output, '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment