-
-
Save tuslo/8da539b4958447cd9b82 to your computer and use it in GitHub Desktop.
Revisions
-
catacgc revised this gist
Oct 27, 2012 . No changes.There are no files selected for viewing
-
catacgc revised this gist
Oct 27, 2012 . 1 changed file with 12 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,28 +4,29 @@ ini_set('memory_limit', '2G'); $data = new MyObject(3000000); benchmark('msgpack', 'msgpack_pack', 'msgpack_unpack', $data); benchmark('serialize', 'serialize', 'unserialize', $data); benchmark('json', function($data) { return json_encode($data); }, function($data) { return json_decode($data, true); }, $data); function benchmark($name, $serialize, $unserialize, $data) { title($name); $filename = "/tmp/data".$name; $file = fopen($filename, "wb"); $time = microtime(true); $serialized = call_user_func($serialize, $data); echo sprintf('Serialization: %f', microtime(true) - $time) . PHP_EOL; fwrite($file, $serialized); fclose($file); echo sprintf('File size %dK', filesize($filename) >> 10) . PHP_EOL; $file = fopen($filename, "rb"); $contents = fread($file, filesize($filename)); $time = microtime(true); $data = call_user_func($unserialize, $contents); -
catacgc created this gist
Oct 27, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ <?php ini_set('memory_limit', '2G'); $data = new MyObject(3000000); title('serialize'); benchmark('serialize', 'unserialize', $data); title('json_encode'); benchmark(function($data) { return json_encode($data); }, function($data) { return json_decode($data, true); }, $data); title('msgpack'); benchmark('msgpack_pack', 'msgpack_unpack', $data); function benchmark($serialize, $unserialize, $data) { $file = fopen("/tmp/data", "wb"); $time = microtime(true); $serialized = call_user_func($serialize, $data); echo sprintf('Serialization: %f', microtime(true) - $time) . PHP_EOL; fwrite($file, $serialized); fclose($file); echo sprintf('File size %dK', filesize("/tmp/data") >> 10) . PHP_EOL; $file = fopen("/tmp/data", "rb"); $contents = fread($file, filesize("/tmp/data")); $time = microtime(true); $data = call_user_func($unserialize, $contents); echo sprintf('Deserialization: %f', microtime(true) - $time) . PHP_EOL; fclose($file); } class MyObject { protected $arr; protected $string; protected $objects = array(); public function __construct($size, $createObjects = true) { $this->arr = range(0, $size); $this->string = implode("", $this->arr); if ($createObjects) { for ($i = 0; $i < 100; $i++) { $this->objects[] = new MyObject(100, false); } } } } function title($title) { echo PHP_EOL.'___________'.PHP_EOL.$title.PHP_EOL.'___________'.PHP_EOL; }