Last active
May 9, 2022 18:12
-
-
Save parf/f05ca955a523657581b55c62fc982059 to your computer and use it in GitHub Desktop.
msgpack vs igbinary comparison on 1500x1500 array. msgpack is ~30 times slower
This file contains 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 | |
ini_set('memory_limit', '32G'); | |
// PARFS speed test | |
echo "PHP Version: ".phpversion()." @ ".gethostname()."\n"; | |
$sz = function ($f, $fu, $data, $iterations=10, $gz=1) { | |
$raw_size = 0; | |
$t0 = microtime(1); | |
foreach (range(1, $iterations) as $XX) { | |
if ($f == 'json_encode') | |
$y = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); | |
else | |
$y = $f($data); | |
if (! $raw_size) | |
$raw_size = strlen($y); | |
if ($gz) | |
$y = gzdeflate($y); | |
} | |
$time = microtime(1) - $t0; | |
$zip_size = strlen($y); | |
$t0 = microtime(1); | |
foreach (range(1, $iterations) as $XX) { | |
if ($gz) | |
$z = gzinflate($y); | |
else | |
$z = $y; | |
if ($f == 'json_encode') | |
$z = json_decode($z, 1); | |
#elseif ($f == 'serialize') | |
# $z = unserialize(gzinflate($y), false); // PHP7 ONLY | |
else | |
$z = $fu($z); | |
} | |
$time2 = microtime(1) - $t0; | |
return str_pad($f.":", 20). | |
" ".str_pad(number_format($raw_size), 10, " ",STR_PAD_LEFT). | |
" ".str_pad(number_format($zip_size), 10, " ",STR_PAD_LEFT). | |
" ".str_pad(number_format($time2 / $iterations, 4), 9, " ",STR_PAD_LEFT). | |
" ".str_pad(number_format($time / $iterations, 4), 10, " ",STR_PAD_LEFT); | |
}; | |
$size = 1500; | |
$data = []; | |
for($i=0; $i<$size; $i++){ | |
for($j=0; $j<$size; $j++){ | |
$data[$i][$j] = [$i, "a$i" => "b$j"]; | |
} | |
} | |
$data_set = "array $size x $size"; | |
$iterations = 2; | |
$mem = number_format(memory_get_peak_usage(1) / 1000000, 1); | |
echo "Data set: `$data_set`, performed $iterations iterations, data-set-memory-usage: ${mem}M\n", | |
"ALGO SIZE-RAW SIZE-GZIP UNPACK1/sec PACK1/sec << time per one iteration", "\n", | |
# $sz("igbinary_serialize", "igbinary_unserialize", $data, $iterations), "\n", | |
$sz("igbinary_serialize", "igbinary_unserialize", $data, $iterations, 0), "\n", | |
# $sz("msgpack_pack", "msgpack_unpack", $data, $iterations), "\n", | |
$sz("msgpack_pack", "msgpack_unpack", $data, $iterations, 0), "\n" | |
# $sz("json_encode", "json_decode", $sd), | |
# $sz("serialize", "unserialize", $sd) | |
; | |
/* | |
RESULTS: | |
> ./igbinary-vs-msgpack.php | |
PHP Version: 5.6.20 @ | |
Data set: `array 1500 x 1500`, performed 2 iterations, data-set-memory-usage: 1,306.0M | |
ALGO SIZE-RAW SIZE-GZIP UNPACK1/sec PACK1/sec << time per one iteration | |
igbinary_serialize: 34,866,787 34,866,787 1.8073 2.1464 | |
msgpack_pack: 34,348,503 34,348,503 51.9373 1.3356 | |
> ./igbinary-vs-msgpack.php | |
PHP Version: 7.0.8 @ | |
Data set: `array 1500 x 1500`, performed 2 iterations, data-set-memory-usage: 1,101.0M | |
ALGO SIZE-RAW SIZE-GZIP UNPACK1/sec PACK1/sec << time per one iteration | |
igbinary_serialize: 34,866,787 34,866,787 3.3561 0.6330 | |
msgpack_pack: 34,348,503 34,348,503 91.5198 1.6248 | |
*/ |
This might depend on the type of data you store.
I just plugged two counters in my live website, calculating time spend in either of them, and how small the object is, and MSGPACK wins from IGBINARY by a small margin on size, about 10%, wins on compression by 10%, but loses on a use-case where data was serialized and unserialized once, MSGPACK was 15% slower. Read ten times more than writing, IGBINARY clearly wins by large margin of 80%.
Not sure why MSGPACK unserialization is so slow.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tested with some standard information (arrays, numbers, and string) and in general, igbinary is at the top. However, not by 30x. 😜
Apparently, msgpack is faster to serialize some kind of information.
https://github.com/EFTEC/php-benchmarks#serializations