Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85813 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 7185 invoked from network); 14 Apr 2015 19:42:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2015 19:42:28 -0000 Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.52 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.218.52 mail-oi0-f52.google.com Received: from [209.85.218.52] ([209.85.218.52:34233] helo=mail-oi0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E3/88-47925-3AD6D255 for ; Tue, 14 Apr 2015 15:42:28 -0400 Received: by oiko83 with SMTP id o83so9649888oik.1 for ; Tue, 14 Apr 2015 12:42:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=akL9PKO/RPqNcLetDSZlP4F4oonP9/AdJhWY1Utf0s4=; b=n0CqIMum3/782vX+koK4QUoc++efKtu9kdu9qXBdnmookXzpLIFWWlT/X+cXBn8zCP 4N9EwncHfdNVX2rLqZtt8yPF5x0/Oih+zXDFKxBUxY4ifs/HFlFPl39XVjJsnUI+ZJpW eCDJ3AF11XFILD6LjB10SoDIAqExWAr1YJRzzH7e4j4etbmtnWvQI+L4Ds0SucOD/Kkr Swaw5vR7E5/OKEE1YWoLy2mAVEyeASA/pB97QDGkshtxXJWO/zkPkwFV4vcU27lSZGEI umD6S85oabLBCloXwrH4Fl3MWqAuz7FcFeeewuzTfHVVaSn/HAMS94jwQwF1ajme7VGt nqzg== X-Received: by 10.202.210.194 with SMTP id j185mr12660226oig.68.1429040544843; Tue, 14 Apr 2015 12:42:24 -0700 (PDT) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.202.104.196 with HTTP; Tue, 14 Apr 2015 12:41:44 -0700 (PDT) In-Reply-To: References: Date: Wed, 15 Apr 2015 04:41:44 +0900 X-Google-Sender-Auth: H3aOXxpqGeS2FiLeAM05LB20rHY Message-ID: To: Danylo Medinsky Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a113df3b60b90200513b46e17 Subject: Re: [PHP-DEV] Re: Potential binary search optimization or feature From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a113df3b60b90200513b46e17 Content-Type: text/plain; charset=UTF-8 Hi Danylo, On Wed, Apr 15, 2015 at 3:23 AM, Danylo Medinsky wrote: > Thanks for the feedback and apologies for the late response. > > Since my posting and your responses, I began implementing the binary search > into array.c. After a couple days of coding and research, I managed to > produce this code. > > HashTable *arr_hash; > arr_hash = Z_ARRVAL_P(array); > int low=0; > int high=zend_hash_num_elements(arr_hash); > int mid; > zval res; > while(low<=high){ > mid=(low+high)/2; > ZVAL_DEREF(entry); > entry = zend_hash_index_find(arr_hash, mid); > compare_function(&res,entry,value); > //php_printf("%ld\n", Z_LVAL(res)); > if(Z_LVAL(res) > 0){ > high=mid-1; > }else if(Z_LVAL(res) < 0){ > low=mid+1; > }else{ > RETURN_TRUE; > } > } > RETURN_FALSE; > > > After benchmarking this code, I managed to conclude that this binary search > algorithm preforms vastly quicker then the php_search_array function. Based > on these results I believe that this algorithm would be a good optimization > or feature to add to PHP. Based on the feedback I have received from you > guys, I am holding off the sort flag in the binary search function > parameters until I can determine how much performance will be compromised > from the sorting. > > At the moment I am trying to figure out if my function would be considered > a minor or major chance since if it is a major/large change, I will require > a RFC in order to submit a pull request. > > If anybody has any additional feedback or recommendations, feel free to > reply to this post. > Since array has state, making it a class makes sense. It would be better if it implemented as array class. You can keep sorted state if you use class. Natural place would be SplArray, but the implementation is overly complex... Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --001a113df3b60b90200513b46e17--