Skip to content

Instantly share code, notes, and snippets.

@dariotilgner
Created December 8, 2021 21:13
Show Gist options
  • Save dariotilgner/61676dff8cd369a73e7074b2dfd4cf66 to your computer and use it in GitHub Desktop.
Save dariotilgner/61676dff8cd369a73e7074b2dfd4cf66 to your computer and use it in GitHub Desktop.
Simple benchmark for file hashes
<?php
// 2011542944 Dec 8 21:58 bigcsvfile.csv
$fileName = '/home/users/dtilgner/bigcsvfile.csv';
echo 'Performance benchmark (2 GB csv file) => md5_file(...) // hash_file("md5", ...) // hash_file("crc32", ...) // shell_exec("md5sum ...")' . PHP_EOL;
$md5FileTimes = [];
$hashFileMd5Times = [];
$hashFileCrc32Times = [];
$md5SumTimes = [];
echo '===================' . PHP_EOL . PHP_EOL . 'md5_file(...)' . PHP_EOL;
for ($i = 0; $i < 100; $i++) {
$start = microtime(true);
md5_file($fileName, true);
$md5FileTimes[] = microtime(true) - $start;
}
echo 'Average run time: ' . (array_sum($md5FileTimes) / count($md5FileTimes)) . PHP_EOL; // Average run time: 4.9545040130615
echo '===================' . PHP_EOL . PHP_EOL . 'hash_file("md5", ...)' . PHP_EOL;
for ($i = 0; $i < 100; $i++) {
$start = microtime(true);
hash_file('md5', $fileName, true);
$hashFileMd5Times[] = microtime(true) - $start;
}
echo 'Average run time: ' . (array_sum($hashFileMd5Times) / count($hashFileMd5Times)) . PHP_EOL; // Average run time: 4.7669960021973
echo '===================' . PHP_EOL . PHP_EOL . 'hash_file("crc32", ...)' . PHP_EOL;
for ($i = 0; $i < 100; $i++) {
$start = microtime(true);
hash_file('crc32', $fileName, true);
$hashFileCrc32Times[] = microtime(true) - $start;
}
echo 'Average run time: ' . (array_sum($hashFileCrc32Times) / count($hashFileCrc32Times)) . PHP_EOL; // Average run time: 7.817800283432
echo '===================' . PHP_EOL . PHP_EOL . 'shell_exec("md5sum ...")' . PHP_EOL;
for ($i = 0; $i < 100; $i++) {
$start = microtime(true);
shell_exec('md5sum -b ' . escapeshellarg($fileName));
$md5SumTimes[] = microtime(true) - $start;
}
echo 'Average run time: ' . (array_sum($md5SumTimes) / count($md5SumTimes)) . PHP_EOL; // Average run time: 3.625156712532
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment