• Posted on March 2, 2014

PHP Calculate Time for Hashing Algorithms

Figuring out the time required for each hashing algorithm in PHP is a simple task of running a string through the hash() function and timing it with microtime(). I needed to do this for all of the available algorithms and sort them from fastest to slowest. Please keep in mind that if hashing for the purpose of passwords, don’t go by which hashing function is the fastest, security can take a few extra thousandths of a second. If you are wondering why this would ever be useful, when time is a really important factor, hashing using the proper algorithm is useful. Hashing is used for more than just passwords. Hashing is often used as “checksums” to verify files and their original source, checking for duplicate files when stored in a database, and more.

	$algos = hash_algos();
	$data = array();
	$string = 'aabbccddeeffgghhiijjkkllmmnnoopp123456789';
	$test_count = 100;

	foreach($algos as $algo) {
		$start = microtime();
		$stats = array(
			'name' => $algo,
			'totaltime' => 0,
			'avgtime' => 0
		for($i = 0; $i < $test_count; $i++) {
		$end = microtime();
		$stats['totaltime'] = $end - $start;
		$stats['avgtime'] = ($stats['totaltime'] / $test_count);

	/* Bubble Sort (poorly coded)
	 * Sorts from Fastest "totaltime" to the slowest
	function sort_time($data) {
		$obj1 = null;
		$obj2 = null;
		$temp = null;
		$changes = 0;
		do {
			$obj1 = $data[0];
			$changes = 0;
			for($i = 1; $i < count($data); $i++) {
				$obj2 = $data[$i];

				if($obj2['totaltime'] < $obj1['totaltime']) {
					$data[$i-1] = $obj2;
					$data[$i] = $obj1;

				$obj1 = $data[$i];
		}while($changes != 0);
		return $data;

	$data = sort_time($data);

	/* Show Table of Timings */
	echo '<table>';
	foreach($data as $d) {
			<td><?php echo $d['name']; ?></td>
			<td><?php echo number_format($d['totaltime'],10); ?></td>
			<td><?php echo number_format($d['avgtime'],10); ?></td>
	echo '</table>';

Code Breakdown

The function hash_algos() creates an array of all available algorithms available for the hash function. Since we want to test all of the algorithms, this is the most effective way. You could of course remove this function and create an array of the algorithms you wish to test.

The next few lines just set up the required variables, such as $data, $string and $test_count. $test_count is how many times to use each algorithm to get a better average timing. $string is used as the string to hash. The reason I didn’t make $string randomly generate is because when focusing on timings for algorithms, you usually have a more specific type of string you want hashed. And last of all, $data is where all of our data will be stored.

The foreach loop now loops through every algorithm stored in our $algos array. We use the value as the first parameter in the hash() function to specify what algorithm to use.

We then start the microtime and loop from 0 to $test_count. Inside the loop we run the hashing function and then keep doing that. At the end of the loop we stop the timer and then calculate the total time and the average time. This data is then pushed into the global $data variable for future use.

Below we have our sort_time function which is a poorly written bubble sort. I needed a way to sort from fastest to slowest “totaltime”.

Finally we print out all of the data in a table. If you are running this code in a terminal, you will probably want to format the output differently so it’s more easily readable.