Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active August 29, 2015 14:23

Revisions

  1. masakielastic revised this gist Jun 28, 2015. 1 changed file with 19 additions and 18 deletions.
    37 changes: 19 additions & 18 deletions benchmark.php
    Original file line number Diff line number Diff line change
    @@ -13,21 +13,22 @@ function timer(callable $block) {

    $buf = '';

    $tests['random_bytes'] = timer(function() {
    $buf = random_bytes(32);
    });

    $tests['openssl_random_pseudo_bytes'] = timer(function() {
    $buf = openssl_random_pseudo_bytes(32);
    });

    $tests['mcrypt_create_iv'] = timer(function() {
    $buf = mcrypt_create_iv(32, MCRYPT_DEV_URANDOM);
    });

    $tests['mt_rand'] = timer(function() {
    $buf = '';
    for ($i = 0; $i < 32; ++$i) {
    $buf .= chr(mt_rand(0, 255));
    }
    });
    $tests = [
    'random_bytes' => timer(function() {
    $buf = random_bytes(32);
    }),
    'openssl_random_pseudo_bytes' => timer(function() {
    $buf = openssl_random_pseudo_bytes(32);
    }),
    'mcrypt_create_iv' => timer(function() {
    $buf = mcrypt_create_iv(32, MCRYPT_DEV_URANDOM);
    }),
    'mt_rand' => timer(function() {
    $buf = '';
    for ($i = 0; $i < 32; ++$i) {
    $buf .= chr(mt_rand(0, 255));
    }
    })
    ];

    var_dump($tests);
  2. masakielastic renamed this gist Jun 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. masakielastic renamed this gist Jun 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. masakielastic renamed this gist Jun 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. masakielastic renamed this gist Jun 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. masakielastic created this gist Jun 27, 2015.
    33 changes: 33 additions & 0 deletions prng_benchmark.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?php
    function timer(callable $block) {
    $start = microtime(true);

    for ($i = 0; $i < 100000; ++$i) {
    $block();
    }

    $end = microtime(true);

    return $end - $start;
    }

    $buf = '';

    $tests['random_bytes'] = timer(function() {
    $buf = random_bytes(32);
    });

    $tests['openssl_random_pseudo_bytes'] = timer(function() {
    $buf = openssl_random_pseudo_bytes(32);
    });

    $tests['mcrypt_create_iv'] = timer(function() {
    $buf = mcrypt_create_iv(32, MCRYPT_DEV_URANDOM);
    });

    $tests['mt_rand'] = timer(function() {
    $buf = '';
    for ($i = 0; $i < 32; ++$i) {
    $buf .= chr(mt_rand(0, 255));
    }
    });
    10 changes: 10 additions & 0 deletions results.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    array(4) {
    ["random_bytes"]=>
    float(0.038606882095337)
    ["openssl_random_pseudo_bytes"]=>
    float(0.26424193382263)
    ["mcrypt_create_iv"]=>
    float(0.42767190933228)
    ["mt_rand"]=>
    float(0.79697513580322)
    }