function bench($value, $n = 1000000) { $benchmark = new Benchmark(); $benchmark->add('serialize', function () use(&$value) { serialize($value); }); $benchmark->add('json_encode', function () use(&$value) { json_encode($value); }); if (function_exists('bin_encode')) { $benchmark->add('bin_encode', function () use(&$value) { bin_encode($value); }); } if (function_exists('bson_encode')) { $benchmark->add('bson_encode', function () use(&$value) { bson_encode($value); }); } if (function_exists('msgpack_pack')) { $benchmark->add('msgpack_pack', function () use(&$value) { msgpack_pack($value); }); } if (function_exists('igbinary_serialize')) { $benchmark->add('igbinary_serialize', function () use(&$value) { igbinary_serialize($value); }); } $benchmark->add('var_export', function () use(&$value) { var_export($value, true); }); $benchmark->setCount($n); $benchmark->run(); }
function bench($value, $n = 1000000) { $benchmark = new Benchmark(); $serialized = serialize($value); $benchmark->add('unserialize', function () use(&$serialized) { unserialize($serialized); }); $jsonEncoded = json_encode($value); $benchmark->add('json_decode', function () use(&$jsonEncoded) { json_decode($jsonEncoded); }); if (function_exists('bin_decode')) { $binEncoded = bin_encode($value); $benchmark->add('bin_decode', function () use(&$binEncoded) { bin_decode($binEncoded); }); } if (function_exists('bson_decode')) { $bsonEncoded = bson_encode($value); $benchmark->add('bson_decode', function () use(&$bsonEncoded) { bson_decode($bsonEncoded); }); } if (function_exists('msgpack_pack')) { $msgPack = msgpack_pack($value); $benchmark->add('msgpack_unpack', function () use(&$msgPack) { msgpack_unpack($msgPack); }); } if (function_exists('igbinary_unserialize')) { $igbinarySerialized = igbinary_serialize($value); $benchmark->add('igbinary_unserialize', function () use(&$igbinarySerialized) { igbinary_unserialize($igbinarySerialized); }); } $benchmark->setCount($n); $benchmark->run(); }