Created
May 6, 2021 17:44
-
-
Save insign/633ad416b46f812206685ee8868f42a3 to your computer and use it in GitHub Desktop.
PHP Benchmark of algorithms
This file contains 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 | |
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; | |
} | |
} |
This file contains 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 | |
// @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"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Laravel Command