Last active
February 29, 2024 21:54
-
-
Save shlomohass/1d20a8dfb00a9031a451b60e19025f73 to your computer and use it in GitHub Desktop.
This test will compare checksum methods with php hashing procedures and some basic ones too -> crc32(), md5(), sha1(), xor(), xor2(), add(). The text is randomly generated each iteration to avoid caching, the test also use several text length that will be different each iteration.
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 | |
/******************************************************************************* | |
* Created by: shlomo hassid. | |
* Release Version : 1.1 | |
* Creation Date: 02/04/2018 | |
* | |
* This test will compare checksum methods with php hashing procedures and some | |
* basic ones to -> crc32(), md5(), sha1(), xor(), xor2(), add(). | |
* The text is randomly generated each iteration to avoid caching, the test also | |
* use several text length that will be defferent each iteration. | |
******************************************************************************/ | |
/******************************************************************************* | |
* Result: on ubuntu 14.04, 4g mem, 4 cores, apache, PHP 7.2.4 | |
PHP CHECKSUM METHODS PERFORMANCE TEST: | |
* Doing are best to avoid cache. | |
* Executed with PHP version: 7.2.4 | |
Results (milliseconds): | |
Total iterations: 10000 | |
Total execution time: 2,083.94599 | |
md5 -> 25.90263 | |
crc32 -> 36.51854 | |
sha1 -> 48.40440 | |
xor -> 274.43411 | |
add -> 322.95360 | |
xor2 -> 455.45782 | |
All text lengths used: | |
10 chars long: 1645 times. | |
100 chars long: 1707 times. | |
500 chars long: 1652 times. | |
1000 chars long: 1676 times. | |
2000 chars long: 1653 times. | |
5000 chars long: 1667 times. | |
******************************************************************************/ | |
/********************** Define Performance Test *****************************/ | |
//Definition Values: | |
$Iterations = 10000; // How many Iterations -> each one is random | |
$strlengths = [100,10,500,1000,2000,5000]; // Will radomize the text length | |
$precision = 5; // Floating point precision for time value | |
//Will store results -> Don't edit | |
$report = [ | |
"iterations" => $Iterations, | |
"totaltime" => 0.0, | |
"methods" => [ | |
"crc32" => 0.0, | |
"md5" => 0.0, | |
"sha1" => 0.0, | |
"xor" => 0.0, | |
"xor2" => 0.0, | |
"add" => 0.0 | |
], | |
"usedlength" => array_combine( | |
$strlengths, | |
array_fill(0,count($strlengths),0) | |
), | |
]; | |
/**************************** Run Test **************************************/ | |
$test_start_time = microtime(true); | |
for ($i=0; $i<$Iterations; $i++) { | |
//Generate random string of random length based on the defined values: | |
$lentouse = mt_rand(0, count($strlengths) - 1); | |
$str = generateRandomString($strlengths[$lentouse]); | |
//Log length used: | |
$report["usedlength"][$strlengths[$lentouse]] += 1; | |
//crc32 execution: | |
$time_start = microtime(true); | |
$x = crc32($str); | |
$time_end = microtime(true); | |
$report["methods"]["crc32"] += round(($time_end-$time_start)*1000,$precision); | |
//md5 execution: | |
$time_start = microtime(true); | |
$x = md5($str); | |
$time_end = microtime(true); | |
$report["methods"]["md5"] += round(($time_end-$time_start)*1000,$precision); | |
//sha1 execution: | |
$time_start = microtime(true); | |
$x = sha1($str); | |
$time_end = microtime(true); | |
$report["methods"]["sha1"] += round(($time_end-$time_start)*1000,$precision); | |
//xor execution: | |
$time_start = microtime(true); | |
$x = func_xor($str); | |
$time_end = microtime(true); | |
$report["methods"]["xor"] += round(($time_end-$time_start)*1000,$precision); | |
//xor2 execution: | |
$time_start = microtime(true); | |
$x = func_xor2($str); | |
$time_end = microtime(true); | |
$report["methods"]["xor2"] += round(($time_end-$time_start)*1000,$precision); | |
//add execution: | |
$time_start = microtime(true); | |
$x = func_add($str); | |
$time_end = microtime(true); | |
$report["methods"]["add"] += round(($time_end-$time_start)*1000,$precision); | |
} | |
//Log total time: | |
$test_end_time = microtime(true); | |
$report["totaltime"] += round(($test_end_time-$test_start_time)*1000,$precision); | |
//Expose results: | |
echo get_final_results($report, $precision); | |
/********************** Random String Generator *****************************/ | |
function generateRandomString($length) { | |
$numberOfbytes = round($length/2, 0, PHP_ROUND_HALF_EVEN); | |
return bin2hex(random_bytes($numberOfbytes)); | |
} | |
/********************* Xor Xor2 add Functions *******************************/ | |
function func_xor($str) { | |
$l = strlen($str); | |
$x = 0x77; | |
for ($j=0; $j<$l; $j++) { $x = $x xor ord($str[$j]); } | |
return $x; | |
} | |
function func_xor2($str) { | |
$l = strlen($str); | |
$x = 0x08; | |
for ($j=0; $j<$l; $j++) { $x = ($x<<2) xor $str[$j]; } | |
return $x; | |
} | |
function func_add($str) { | |
$l = strlen($str); | |
$x = 0; | |
for ($j=0; $j<$l; $j++) { $x = $x + ord($str[$j]); } | |
return $x; | |
} | |
/**************************** Print Functions *******************************/ | |
function get_final_results($report, $precision) { | |
//Sort results: | |
asort($report["methods"]); | |
ksort($report["usedlength"]); | |
$r = "<br /><pre>PHP CHECKSUM METHODS PERFORMANCE TEST:" | |
."\n * Doing are best to avoid cache." | |
."\n * Executed with PHP version: ".phpversion() | |
."\n\nResults (milliseconds):" | |
."\n\n Total iterations: ". $report["iterations"] | |
."\n Total execution time: ". number_format($report["totaltime"], $precision)."\n"; | |
foreach ($report["methods"] as $m => $t) { | |
$r .= "\n".str_repeat(" ",4) | |
.$m.str_repeat(" ",5-strlen($m)+4)."->" | |
.str_repeat(" ",4) | |
.number_format($t, $precision); | |
} | |
$r .= "\n\nAll Text lengths used:"; | |
foreach ($report["usedlength"] as $k => $c) { | |
$r .= "\n ".$k." chars long: \t".$c." times."; | |
} | |
return $r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment