php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79023 very slow hash('crc32b', ...)
Submitted: 2019-12-24 00:07 UTC Modified: 2020-07-20 14:06 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: bugzilla77 at gmail dot com Assigned: cmb (profile)
Status: Closed Package: hash related
PHP Version: 7.4.1 OS: Windows 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
32 + 13 = ?
Subscribe to this entry?

 
 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-24 10:30 UTC] cmb@php.net
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().
 [2019-12-30 17:31 UTC] nikic@php.net
Created https://github.com/php/php-src/pull/5044 to improve hash() performance. With that and a larger tripcount I get:

0.31190896034241 sec
0.48243403434753 sec
0.46064209938049 sec

Getting full parity isn't really possible because a) hash() is dynamic, it needs to lookup the algorithm and dispatch to it and b) hash() returns a string, while crc32() returns an integer.
 [2019-12-31 04:42 UTC] bugreports at gmail dot com
crc32 could be way faster with a native/avx implementation like base64 got in PHP 7.3 looking at the output of mysqld "InnoDB: Using SSE2 crc32 instructions"

currently it's even slower than sha1

and xxhash should be in the official hash-extension
https://create.stephan-brumme.com/xxhash/
https://github.com/stuartherbert/php-xxhash

md4:         0.15917
joaat:       0.27944
md5:         0.28036
sha1:        0.38227
crc32:       0.46766
sha3-224:    0.55059
sha3-256:    0.61051
crc32b:      0.64266
crc32c:      0.64455
adler32:     0.70149
sha3-384:    0.75485
sha224:      1.02762
sha256:      1.02943
sha3-512:    1.07797
sha384:      1.13733
sha512/256:  1.143
sha512/224:  1.14454
sha512:      1.14697
whirlpool:   1.76455
 [2020-07-20 14:06 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-07-20 14:06 UTC] cmb@php.net
<https://github.com/php/php-src/pull/5044> has been merged, so
this looks good as bugfix to me.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 10:01:28 2024 UTC