|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2019-12-24 00:07 UTC] bugzilla77 at gmail dot com
 Description:
------------
very slow hash('crc32b', ...)
Test script:
---------------
<?php
 $i=2000000;
 $x=sha1(microtime(1).mt_rand());
 $time_a=microtime(true);
 for($z=0;$z<$i;$z++){
  $a=crc32($x);
 }
 $time_a=microtime(true)-$time_a;
 $time_b=microtime(true);
 for($z=0;$z<$i;$z++){
  $b=hash('crc32b',$x);
 }
 $time_b=microtime(true)-$time_b;
 $time_c=microtime(true);
 for($z=0;$z<$i;$z++){
  $c=hash('crc32b',$x,true);
 }
 $time_c=microtime(true)-$time_c;
 print($time_a.' sec');
 print('<br/>');
 print($time_b.' sec');
 print('<br/>');
 print($time_c.' sec');
?>
Expected result:
----------------
0.34135699272156 sec
<=0.34135699272156 sec
<=0.34135699272156 sec
Actual result:
--------------
0.34135699272156 sec
0.77139401435852 sec
0.80676794052124 sec
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 14:00:01 2025 UTC | 
Firstly, crc32($x) doesn't call hash('crc32b', $x), so the assumption that the latter should be equally fast as the former (or even faster) is wrong. Actually, these functions share only the actual hashing primitives. Since hash() has to first map the given string to the desired algorithm, and uses function pointers subsequently, it is supposed to be somewhat slower. However, hash() still uses classic ZPP, while crc32() uses the newer fast ZPP macros, what causes an additional performance penalty. Basically, the same applies to sha1() and md5().