Skip to content

Instantly share code, notes, and snippets.

@parf
Last active May 9, 2022 18:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parf/f05ca955a523657581b55c62fc982059 to your computer and use it in GitHub Desktop.
Save parf/f05ca955a523657581b55c62fc982059 to your computer and use it in GitHub Desktop.
msgpack vs igbinary comparison on 1500x1500 array. msgpack is ~30 times slower
<?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
*/
@mitsh
Copy link

mitsh commented Feb 20, 2021

Hmm...

PHP Version: 8.0.2 @
Data set: `array 1500 x 1500`, performed 2 iterations, data-set-memory-usage: 1,103.1M
ALGO                   SIZE-RAW  SIZE-GZIP  UNPACK1/sec PACK1/sec  << time per one iteration
igbinary_serialize:  34,866,787 34,866,787    0.5944     1.5012
msgpack_pack:        34,348,503 34,348,503    0.5974     0.8788

@parf
Copy link
Author

parf commented Jul 1, 2021

great results

> php igbinary-vs-msgpack.php 
PHP Version: 8.0.8 @
Data set: `array 1500 x 1500`, performed 10 iterations, data-set-memory-usage: 1,103.1M
ALGO                   SIZE-RAW  SIZE-GZIP  UNPACK1/sec PACK1/sec  << time per one iteration
igbinary_serialize:  34,866,787 34,866,787    0.3245     0.8300
msgpack_pack:        34,348,503 34,348,503    0.4145     0.7114

model name      : AMD Ryzen 7 2700X Eight-Core Processor

> rpm -qa | grep msgp
php-pecl-msgpack-2.1.2-1.fc33.remi.8.0.x86_64
msgpack-3.1.0-6.fc33.x86_64

@jorgecc
Copy link

jorgecc commented Dec 9, 2021

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

@t0mm1e
Copy link

t0mm1e commented May 9, 2022

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