Re: An optimization idea
| From: | shire | Date: | Mon, 20 Oct 2008 20:09:34 +0000 |
| Subject: | Re: An optimization idea | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-41291@lists.php.net to get a copy of this message | ||
On Oct 19, 2008, at 12:11 PM, Karoly Negyesi wrote:
Hi, I think zend_hash_compare could get a healthy speed boost in some cases if first it would check whether the two variables passed to it are actually the same (because of "reference counting"). Sorry, my C skills are way too rusty to write the patch which is likely to be just a few lines long.A quick patch/test seems to agree with you, I'd be interested to know if you/others are able to apply this patch and see any real-world savings with your web application. The following was done with a debug build of PHP so results are likely exaggerated. I'm also using non-recursive array with 256 byte strings below, results with numeric values are less significant of course (approx a 25% gain)... I'll also look into applying this to some other functions like compare_function where it's likely to yield a larger savings in a general application. patch against php-5.2 CVS head -------------------------- iff --git a/Zend/zend_hash.c b/Zend/zend_hash.c index a1d7071..d11785f 100644 --- a/Zend/zend_hash.c +++ b/Zend/zend_hash.c @@ -1327,16 +1327,14 @@ ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t co
IS_CONSISTENT(ht1);
IS_CONSISTENT(ht2);
- HASH_PROTECT_RECURSION(ht1); - HASH_PROTECT_RECURSION(ht2);-
result = ht1->nNumOfElements - ht2->nNumOfElements;
- if (result!=0) {
- HASH_UNPROTECT_RECURSION(ht1);
- HASH_UNPROTECT_RECURSION(ht2);
+ if (ht1 == ht2 || result!=0) {
return result;
}
+ HASH_PROTECT_RECURSION(ht1); + HASH_PROTECT_RECURSION(ht2);+
p1 = ht1->pListHead;
if (ordered) {
p2 = ht2->pListHead;
simple test script
-------------------------------
<?php
$arr1 = array();
for ($i=0; $i < 10000; $i++) {
$arr1[$i] = str_repeat('x', 256);
}
$arr2 = array();
for ($i=0; $i < 10000; $i++) {
$arr2[$i] = str_repeat('x', 256);
}
$arr3 = $arr1;
$count = 0;
$start = microtime(true);
for ($i = 0; $i < 1000; $i++) {
if ($arr1 == $arr2) {
$count++;} } $stop = microtime(true); echo "different array time: ".($stop-$start)."\n"; echo "count: $count \n"; $count = 0; $start = microtime(true); for ($i = 0; $i < 1000; $i++) { if ($arr1 == $arr3) {
$count++;} } $stop = microtime(true); echo "identical array time: ".($stop-$start)."\n"; echo "count: $count \n"; ------------------------------- (un-patched php build) shire@shirebook:~/data/php/git/php$ ./sapi/cli/php.vanilla test.php different array time: 4.2019698619843 count: 1000 identical array time: 2.4957029819489 count: 1000 (patched build) shire@shirebook:~/data/php/git/php$ ./sapi/cli/php test.php different array time: 4.059928894043 count: 1000 identical array time: 0.00043511390686035 count: 1000 -shire