Skip to content

Instantly share code, notes, and snippets.

@insign
Created May 6, 2021 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insign/633ad416b46f812206685ee8868f42a3 to your computer and use it in GitHub Desktop.
Save insign/633ad416b46f812206685ee8868f42a3 to your computer and use it in GitHub Desktop.
PHP Benchmark of algorithms
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Benchmark extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bm';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Benchmark test';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$times = [];
$samples = range(-200000, 200000);
$algos = hash_algos();
$progressBar = $this->output->createProgressBar(count($algos));
$progressBar->start();
$progressBar->setFormat('verbose');
foreach ($algos as $algo) {
$startTime = microtime(TRUE);
foreach ($samples as $sample) {
$hash = hash($algo, $sample);
}
$endTime = microtime(TRUE);
$time = number_format($endTime - $startTime , 4);
$sample = strlen($hash) > 20 ? substr($hash,0,20)."..." : $hash;
$times[$time] = [$algo, $time, $sample, strlen($hash)];
$progressBar->advance();
}
$progressBar->finish();
ksort($times);
$this->newLine(2);
$this->table(['Algo', 'Time', 'Sample', 'Size'], $times);
return 0;
}
}
<?php
// @see https://3v4l.org/YGqWt#output
$times = [];
$samples = range(-5000, 5000);
$algos = hash_algos();
foreach ($algos as $algo) {
$startTime = microtime(TRUE);
foreach ($samples as $sample) {
$hash = hash($algo, $sample);
}
$endTime = microtime(TRUE);
$time = number_format($endTime - $startTime , 8);
$times[$algo] = $time;
}
asort($times);
foreach ($times as $algo => $time) {
$sample = hash($algo, $time);
echo "$algo\t$time\t$sample\n";
}
@insign
Copy link
Author

insign commented May 6, 2021

Laravel Command

❯ art bm

 53/53 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% 15 secs

+-------------+--------+-------------------------------------------------------+------+
| Algo        | Time   | Sample                                                | Size |
+-------------+--------+-------------------------------------------------------+------+
| adler32     | 0.0416 | 04020123                                              | 8    |
| crc32       | 0.0435 | 57e6c372                                              | 8    |
| fnv164      | 0.0448 | ffa707bc8b33af37                                      | 16   |
| crc32b      | 0.0465 | 748ea4ed                                              | 8    |
| fnv1a32     | 0.0470 | 68fd614f                                              | 8    |
| crc32c      | 0.0497 | 82de5664                                              | 8    |
| fnv1a64     | 0.0563 | 1c38f55eacac412f                                      | 16   |
| fnv132      | 0.0576 | 816d0b17                                              | 8    |
| md4         | 0.0834 | fb4d7c0203ebc14ebf75a6c61d52e500                      | 32   |
| joaat       | 0.0854 | f136a227                                              | 8    |
| md5         | 0.0868 | 03e6c61603f6c550ab49ab6a2d83f793                      | 32   |
| tiger128,3  | 0.0973 | 229c3001bb04f091319ecd025f29c201                      | 32   |
| tiger192,3  | 0.1061 | 229c3001bb04f091319ecd025f29c2016221c2ffeb40a8fe      | 48   |
| sha1        | 0.1155 | d8cd7d68c3532d53f623ea1ad64c4d6608ee98ae              | 40   |
| tiger160,3  | 0.1185 | 229c3001bb04f091319ecd025f29c2016221c2ff              | 40   |
| tiger128,4  | 0.1250 | 8bb8e000ef64b6972a660f9786f917a4                      | 32   |
| tiger160,4  | 0.1282 | 8bb8e000ef64b6972a660f9786f917a413251b2b              | 40   |
| tiger192,4  | 0.1292 | 8bb8e000ef64b6972a660f9786f917a413251b2b89177502      | 48   |
| ripemd128   | 0.1425 | 57281b3979a914fe2214c01890733c57                      | 32   |
| ripemd256   | 0.1588 | 8ed9a673f8aab63abcd697ff461aac7afd14241979ec559ef5... | 64   |
| ripemd160   | 0.1872 | 20ac59e2411543533cbdfc6d291d5d648403a479              | 40   |
| sha256      | 0.1948 | b552e632666bbf6125e3109e28a4fecc08340d017e44109dad... | 64   |
| sha224      | 0.1977 | d42e529df968e2ce21dff60aa95f34ed6e6f00581229322a2d... | 56   |
| ripemd320   | 0.1988 | 66313fb7dc7c535d997bf7864d5f7b43acc869ffac42c46638... | 80   |
| sha3-256    | 0.2473 | e38b887e2ddfbfd4cf406db42126e68e97c0cc3c8eb8ac57d7... | 64   |
| sha3-512    | 0.2560 | 4275d88c02e3fcfb553622d071ffe8bf2860eaeaedcf09b459... | 128  |
| sha512/256  | 0.2594 | 79bef0640ae9808b7e3dd27e35d57900cfadad108e60937775... | 64   |
| sha512/224  | 0.2617 | c6d6805ec79780d118c7720bbf8ea4e51dc32b5e1867567301... | 56   |
| sha384      | 0.2629 | e0a0792e80bd1e2f3a4a97f4072c9d52f2fe14b052d16b3d04... | 96   |
| sha3-224    | 0.2630 | 78fad08093d790fa4c450b6be9d8342c31a297d25b4c81e426... | 56   |
| haval256,3  | 0.2634 | ea75747688a699b1630d1f5de33fce0a03a2c9e4bb10553f6f... | 64   |
| sha3-384    | 0.2639 | d4987d1410c37ff5107c56545ee40028acf4b51c92192dd8a5... | 96   |
| haval224,3  | 0.2702 | aff3cf13585bac75e466e0fe560effdd75610f07846a7376d2... | 56   |
| sha512      | 0.2763 | 1bd9d26475927fd822a06c459e299e8e25f2ab987150bfc2c3... | 128  |
| haval160,3  | 0.3022 | e649d2428eb84770f3219563efc1f77ad10d0012              | 40   |
| haval192,3  | 0.3348 | dcb09865cc38089fef8b82bc68587568e3ce52fcae65869c      | 48   |
| haval160,4  | 0.3436 | 8c644e3423fdda12ed43e2741d33308364609bce              | 40   |
| haval224,4  | 0.3682 | 89231c6c5dbc3282f2ffcc4635c22296d581ab9891e2e42f5e... | 56   |
| haval192,4  | 0.3691 | 7a10fd9352813158883c86639b01db6a9f34518e826567ca      | 48   |
| haval256,4  | 0.3751 | d43c91a59c4fdca2aa2bc2f6b46472e8d2bff744b472b10e70... | 64   |
| haval128,4  | 0.4044 | 31971fe6277866bbe88b3081b531227c                      | 32   |
| haval160,5  | 0.4250 | d3f9306d56390e76751f11df92013d7ff41725ee              | 40   |
| haval256,5  | 0.4351 | acfe7d23e5b0bc50199204757030eb03e37bd820d556e7881e... | 64   |
| haval192,5  | 0.4367 | e757f250ddb4f1254927dfe6b28eb02f9d9bfb3a8a1bb7eb      | 48   |
| haval224,5  | 0.4387 | ee79c17686d80184658eae6f8d480c7ed856fcc65a9a0bf7c8... | 56   |
| haval128,3  | 0.4557 | 5adb5f8949e4285982debddad084d3c1                      | 32   |
| haval128,5  | 0.4689 | ad842623f37317ed79d84102921f0061                      | 32   |
| gost-crypto | 0.6401 | 331e58a501aa5547aa6c722d36c14ed09d0f386254fad66ffc... | 64   |
| gost        | 0.6420 | eeffb80762857effed581db603c2327d8b5bcbe26e1f0bf883... | 64   |
| snefru256   | 0.8716 | af0c73382976012fdf7f283193b6b5bf5a1265cce16f1c0f2e... | 64   |
| snefru      | 0.8902 | af0c73382976012fdf7f283193b6b5bf5a1265cce16f1c0f2e... | 64   |
| md2         | 1.4094 | 970c004a0dec904005961aeb9ce0d43b                      | 32   |
+-------------+--------+-------------------------------------------------------+------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment