Bug #79023 [Opn]: very slow hash('crc32b', ...)
| From: | nikic@php.net | Date: | Mon, 30 Dec 2019 17:31:17 +0000 |
| Subject: | Bug #79023 [Opn]: very slow hash('crc32b', ...) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-224624@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79023&edit=1
ID: 79023
Updated by: nikic@php.net
Reported by: bugzilla77 at gmail dot com
Summary: very slow hash('crc32b', ...)
Status: Open
Type: Bug
Package: hash related
Operating System: Windows 10
PHP Version: 7.4.1
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2019-12-24 10:32:09] cmb@php.net
Related To: Bug #79024
------------------------------------------------------------------------
[2019-12-24 10:30:53] 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-24 00:07:44] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79023&edit=1