-
-
Save tuslo/8da539b4958447cd9b82 to your computer and use it in GitHub Desktop.
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 characters
| <?php | |
| 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); | |
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment