Hi Danylo, On Wed, Apr 15, 2015 at 3:23 AM, Danylo Medinsky <medinsk...@gmail.com> 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 yohg...@ohgaki.net