Skip to content

Instantly share code, notes, and snippets.

@rybakit
Last active June 24, 2016 10:07
Show Gist options
  • Save rybakit/59467489f2c424726b9411968b5b3b7a to your computer and use it in GitHub Desktop.
Save rybakit/59467489f2c424726b9411968b5b3b7a to your computer and use it in GitHub Desktop.
msgpack pure vs pecl big array
<?php
// @see https://github.com/msgpack/msgpack-php/issues/90
ini_set('memory_limit', '32G');
require __DIR__.'/../vendor/autoload.php';
if (extension_loaded('xdebug')) {
echo "The benchmark must be run with xdebug extension disabled.\n";
exit(42);
}
function pecl($n, $data) {
for ($i = 0; $i < $n; ++$i) {
$packed = \msgpack_pack($data);
$raw = \msgpack_unpack($packed);
}
return $raw;
}
function pure($n, $data) {
global $packer;
global $unpacker;
for ($i = 0; $i < $n; ++$i) {
$packed = $packer->pack($data);
$raw = $unpacker->reset($packed)->unpack();
}
return $raw;
}
function empty_loop($n) {
for ($i = 0; $i < $n; ++$i) {
}
}
function getmicrotime()
{
$t = gettimeofday();
return ($t['sec'] + $t['usec'] / 1000000);
}
function start_test()
{
ob_start();
return getmicrotime();
}
function end_test($start, $name, $overhead = null)
{
global $total;
global $last_time;
$end = getmicrotime();
ob_end_clean();
$last_time = $end-$start;
$total += $last_time;
$num = number_format($last_time,3);
$pad = str_repeat(" ", 24-strlen($name)-strlen($num));
if (is_null($overhead)) {
echo $name.$pad.$num."\n";
} else {
$num2 = number_format($last_time - $overhead,3);
echo $name.$pad.$num." ".$num2."\n";
}
ob_start();
return getmicrotime();
}
function total()
{
global $total;
$pad = str_repeat("-", 24);
echo $pad."\n";
$num = number_format($total,3);
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
echo "Total".$pad.$num."\n";
}
const N = 1;
$size = 1500;
$data = [];
for($i=0; $i<$size; $i++){
for($j=0; $j<$size; $j++){
$data[$i][$j] = [$i, "a$i" => "b$j"];
}
}
$packer = new \MessagePack\Packer();
$unpacker = new \MessagePack\BufferUnpacker();
$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
$result = pecl(N, $data);
$t = end_test($t, 'pecl', $overhead);
assert($result === $data);
$result = pure(N, $data);
$t = end_test($t, 'pure', $overhead);
assert($result === $data);
total();
<?php
// @see https://github.com/msgpack/msgpack-php/issues/90
ini_set('memory_limit', '32G');
require __DIR__.'/../vendor/autoload.php';
if (extension_loaded('xdebug')) {
echo "The benchmark must be run with xdebug extension disabled.\n";
exit(42);
}
function pecl_pack($n, $raw) {
for ($i = 0; $i < $n; ++$i) {
\msgpack_pack($raw);
}
}
function pecl_unpack($n, $packed) {
for ($i = 0; $i < $n; ++$i) {
\msgpack_unpack($packed);
}
}
function pecl_unpack_buf($n, $packed) {
global $pecl_unpacker;
for ($i = 0; $i < $n; ++$i) {
$pecl_unpacker->feed($packed);
$pecl_unpacker->execute();
$pecl_unpacker->data();
}
}
function pure_pack($n, $raw) {
global $packer;
for ($i = 0; $i < $n; ++$i) {
$packer->pack($raw);
}
}
function pure_unpack($n, $packed) {
global $unpacker;
for ($i = 0; $i < $n; ++$i) {
$unpacker->reset($packed)->unpack();
}
}
function empty_loop($n) {
for ($i = 0; $i < $n; ++$i) {
}
}
function getmicrotime()
{
$t = gettimeofday();
return ($t['sec'] + $t['usec'] / 1000000);
}
function start_test()
{
ob_start();
return getmicrotime();
}
function end_test($start, $name, $overhead = null)
{
global $total;
global $last_time;
$end = getmicrotime();
ob_end_clean();
$last_time = $end-$start;
$total += $last_time;
$num = number_format($last_time,3);
$pad = str_repeat(" ", 24-strlen($name)-strlen($num));
if (is_null($overhead)) {
echo $name.$pad.$num."\n";
} else {
$num2 = number_format($last_time - $overhead,3);
echo $name.$pad.$num." ".$num2."\n";
}
ob_start();
return getmicrotime();
}
function total()
{
global $total;
$pad = str_repeat("-", 24);
echo $pad."\n";
$num = number_format($total,3);
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
echo "Total".$pad.$num."\n";
}
const N = 1;
$size = 1500;
$data = [];
for($i=0; $i<$size; $i++){
for($j=0; $j<$size; $j++){
$data[$i][$j] = [$i, "a$i" => "b$j"];
}
}
$packer = new \MessagePack\Packer();
$unpacker = new \MessagePack\BufferUnpacker();
$pecl_unpacker = new \MessagePackUnpacker(true);
$packed = $packer->pack($data);
$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
pecl_pack(N, $data);
$t = end_test($t, 'pecl_pack', $overhead);
pecl_unpack(N, $packed);
$t = end_test($t, 'pecl_unpack', $overhead);
pecl_unpack_buf(N, $packed);
$t = end_test($t, 'pecl_unpack_buf', $overhead);
pure_pack(N, $data);
$t = end_test($t, 'pure_pack', $overhead);
pure_unpack(N, $packed);
$t = end_test($t, 'pure_unpack', $overhead);
total();
$ php tests/bench_big_array.php
empty_loop 0.000
pecl 119.660 119.660
pure 26.962 26.962
------------------------
Total 146.622
$ php -v
PHP 7.0.5 (cli) (built: Apr 23 2016 10:48:01) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
$ php tests/bench_big_array.php
empty_loop 0.000
pecl_pack 1.594 1.594
pecl_unpack 103.036 103.036
pecl_unpack_buf 90.596 90.596
pure_pack 6.363 6.363
pure_unpack 5.042 5.042
------------------------
Total 206.632
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment