Skip to content

Instantly share code, notes, and snippets.

@denji
Created February 12, 2014 14:05
Show Gist options
  • Save denji/7c3c9d4b449f2699ec73 to your computer and use it in GitHub Desktop.
Save denji/7c3c9d4b449f2699ec73 to your computer and use it in GitHub Desktop.
msgpack benchmark
<?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\r\n";
$_templateDecode = "%s [%s]: %s time to decode\r\n";
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
);
/**
* msgpack
*/
$_msgpackStart = microtime(TRUE);
for ($_counter = 0; $_counter < $_maxLoop; $_counter++)
{
msgpack_pack($_source);
}
$_msgpackEnd = microtime(TRUE);
$_msgpackOutput = msgpack_pack($_source);
$_output .= sprintf(
$_templateEncode,
'msgpack_pack()',
$_area,
strlen($_msgpackOutput),
$_msgpackEnd - $_msgpackStart
);
$_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);
$_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);
$_output .= sprintf(
$_templateDecode,
'igbinary_unserialize()',
$_area,
$_igbinaryEnd - $_igbinaryStart
);
/**
* msgpack
*/
$_data = msgpack_pack($_source);
$_msgpackStart = microtime(TRUE);
for ($_counter = 0; $_counter < $_maxLoop; $_counter++)
{
msgpack_unpack($_data);
}
$_msgpackEnd = microtime(TRUE);
$_output .= sprintf(
$_templateDecode,
'msgpack_unpack()',
$_area,
$_msgpackEnd - $_msgpackStart
);
$_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