Last active
December 1, 2020 00:03
-
-
Save bwg/bb9e15bb2e33074257f5 to your computer and use it in GitHub Desktop.
testing serialize vs. json_encode vs. var_export vs. msgpack_pack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHP Version 5.4.30 | |
-------------------------------------------------- | |
Testing 10 item array over 1,000 iterations: | |
serialize 2.1979808807373 ms | |
json_encode 1.3420581817627 ms | |
var_export 1.9409656524658 ms | |
msgpack_pack 1.5850067138672 ms | |
-------------------------------------------------- | |
Testing 100 item array over 1,000 iterations: | |
serialize 15.254020690918 ms | |
json_encode 3.3650398254395 ms | |
var_export 9.0441703796387 ms | |
msgpack_pack 3.3900737762451 ms | |
-------------------------------------------------- | |
Testing 1,000 item array over 1,000 iterations: | |
serialize 106.03904724121 ms | |
json_encode 23.813962936401 ms | |
var_export 79.192876815796 ms | |
msgpack_pack 21.929979324341 ms | |
-------------------------------------------------- | |
Testing 10,000 item array over 1,000 iterations: | |
serialize 1282.6027870178 ms | |
json_encode 245.68605422974 ms | |
var_export 825.11210441589 ms | |
msgpack_pack 219.96188163757 ms | |
-------------------------------------------------- | |
Testing 50,000 item array over 1,000 iterations: | |
serialize 7461.64894104 ms | |
json_encode 1245.4879283905 ms | |
var_export 5308.2621097565 ms | |
msgpack_pack 1060.5230331421 ms | |
-------------------------------------------------- | |
Testing 100,000 item array over 1,000 iterations: | |
serialize 18343.793153763 ms | |
json_encode 2593.523979187 ms | |
var_export 9992.4139976501 ms | |
msgpack_pack 2282.3920249939 ms | |
------------------------------------------------------- | |
average (ms) total (ms) | |
------------------------------------------------------- | |
serialize 4535.2559884389 27211.535930634 | |
json_encode 685.53650379181 4113.2190227509 | |
var_export 2702.6610374451 16215.96622467 | |
msgpack_pack 598.29699993134 3589.781999588 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$iterations = 1000; | |
$sizes = [10, 100, 1000, 10000, 50000, 100000]; | |
$totals = [ | |
'serialize' => 0, | |
'json_encode' => 0, | |
'var_export' => 0, | |
'msgpack_pack' => 0, | |
]; | |
echo "PHP Version ".phpversion()."\n\n"; | |
foreach ($sizes as $size) { | |
$source = array_fill(0, $size, true); | |
echo str_repeat('-', 50)."\n"; | |
echo "Testing ".number_format($size)." item array over ".number_format($iterations)." iterations:\n\n"; | |
// -- serialize ----------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
$out = serialize($source); | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['serialize'] += $duration; | |
echo str_pad("serialize", 20)."{$duration} ms\n"; | |
// -- json_encode --------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
$out = json_encode($source); | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['json_encode'] += $duration; | |
echo str_pad("json_encode", 20)."{$duration} ms\n"; | |
// -- var_export --------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
$out = var_export($source, true); | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['var_export'] += $duration; | |
echo str_pad("var_export", 20)."{$duration} ms\n"; | |
// -- msgpack_pack -------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
$out = msgpack_pack($source); | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['msgpack_pack'] += $duration; | |
echo str_pad("msgpack_pack", 20)."{$duration} ms\n"; | |
} | |
echo "\n".str_repeat('-', 55)."\n"; | |
echo str_pad('', 15); | |
echo str_pad('average (ms)', 20); | |
echo str_pad('total (ms)', 20)."\n"; | |
echo str_repeat('-', 55)."\n"; | |
foreach ($totals as $name => $total) { | |
echo str_pad($name, 15); | |
echo str_pad($total/count($sizes), 20); | |
echo str_pad($total, 20)."\n"; | |
} | |
echo "\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment