Skip to content

Instantly share code, notes, and snippets.

@tuslo
Forked from catacgc/Serialization benchmarks
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save tuslo/8da539b4958447cd9b82 to your computer and use it in GitHub Desktop.

Select an option

Save tuslo/8da539b4958447cd9b82 to your computer and use it in GitHub Desktop.

Revisions

  1. @catacgc catacgc revised this gist Oct 27, 2012. No changes.
  2. @catacgc catacgc revised this gist Oct 27, 2012. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions Serialization benchmarks
    Original file line number Diff line number Diff line change
    @@ -4,28 +4,29 @@ ini_set('memory_limit', '2G');

    $data = new MyObject(3000000);

    title('serialize');
    benchmark('serialize', 'unserialize', $data);
    benchmark('msgpack', 'msgpack_pack', 'msgpack_unpack', $data);

    title('json_encode');
    benchmark(function($data) { return json_encode($data); }, function($data) { return json_decode($data, true); }, $data);
    benchmark('serialize', 'serialize', 'unserialize', $data);

    title('msgpack');
    benchmark('msgpack_pack', 'msgpack_unpack', $data);
    benchmark('json', function($data) { return json_encode($data); }, function($data) { return json_decode($data, true); }, $data);

    function benchmark($serialize, $unserialize, $data)
    function benchmark($name, $serialize, $unserialize, $data)
    {
    $file = fopen("/tmp/data", "wb");
    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("/tmp/data") >> 10) . PHP_EOL;
    echo sprintf('File size %dK', filesize($filename) >> 10) . PHP_EOL;

    $file = fopen("/tmp/data", "rb");
    $contents = fread($file, filesize("/tmp/data"));
    $file = fopen($filename, "rb");
    $contents = fread($file, filesize($filename));

    $time = microtime(true);
    $data = call_user_func($unserialize, $contents);
  3. @catacgc catacgc created this gist Oct 27, 2012.
    56 changes: 56 additions & 0 deletions Serialization benchmarks
    Original 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;
    }