Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85540 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 12497 invoked from network); 30 Mar 2015 06:24:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Mar 2015 06:24:24 -0000 Authentication-Results: pb1.pair.com header.from=medinsky94@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=medinsky94@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.54 as permitted sender) X-PHP-List-Original-Sender: medinsky94@gmail.com X-Host-Fingerprint: 209.85.218.54 mail-oi0-f54.google.com Received: from [209.85.218.54] ([209.85.218.54:35257] helo=mail-oi0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AE/20-09456-81CE8155 for ; Mon, 30 Mar 2015 01:24:24 -0500 Received: by oiag65 with SMTP id g65so119463450oia.2 for ; Sun, 29 Mar 2015 23:24:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=mNiz9/BCu7bvmlY6ySWZdZNJ0LEQO+usfYPP5RSzukQ=; b=D1dPMffAcnUS37yj+j7rrVeR9zK85FA7sCFL33WsrvFuUcOF8WkCbp3yu68BzKoXui JM+sAueAD7zn5q2xQn0mGhKVnVc/6Rr36fy/Osn8TXl5YL7Dun87GNA57ZkS8POq8uyt RWE2wZ42OvEqpIOw7iWEEXDaJj9Btfh41csyMhogMA2wNfAnpwuEfdywfhjJ3M4ha2It KnUPxZ3/WOajjmg4fta/lQa5VTY3ZAzGMEzjQGTURnJqDr5GnRIkGubr2WX/+VG41kPq H4dBmWcXHB7ou4e8fhBMXDRGbIdZT0xGtU3PztxUN92DjdMDzcjY/UmuhHlPChEG7RNC x7jg== MIME-Version: 1.0 X-Received: by 10.202.183.213 with SMTP id h204mr24020838oif.27.1427696661999; Sun, 29 Mar 2015 23:24:21 -0700 (PDT) Received: by 10.202.172.88 with HTTP; Sun, 29 Mar 2015 23:24:21 -0700 (PDT) Date: Sun, 29 Mar 2015 23:24:21 -0700 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=001a113cd16e62b7b205127b8892 Subject: Potential binary search optimization or feature From: medinsky94@gmail.com (Danylo Medinsky) --001a113cd16e62b7b205127b8892 Content-Type: text/plain; charset=UTF-8 Recently, as part of school course on optimization, I have identified a potential optimization or feature for PHP. After looking through the array.c file(located in etc/standard in the PHP source code), I noticed that both the in_array and array_search are using the linear search C function php_search_array. The potential issue I see is that even though the linear search algorithm does not drastically effect performance when searching a relatively small array, it may slow performance down when iterating through a very large array(couple of million indexes). To back this fact up I preformed some benchmarks using a PHP script that measures the time it takes to preform a linear search and binary search. The benchmark was in favor of the binary search as it found its target many times quicker compared to the linear search. After researching more about different PHP search functions, I was not able to find one that uses a binary search algorithm. Based on these factors I believe that a binary search implementation into PHP will prove useful in certain situations and would like to implement that feature. At the moment I am debating between adding a separate binary search based function into array.c or modifying the current array_search_array function to utilize a binary search algorithm. Since the requirement for a binary search is that the array being searched is sorted, I thought is would be a good idea to have a Boolean parameter in the binary search function to specify if the input array should be sorted or not. something among the lines of "function binary_search(string target, array arrayToSearch, bool sort)". Doing this, the function can preform a sort if required to, thus not needing to sort a array that is already sorted. Before I can begin the implementation and create a pull request, I would like to know the PHP's community feedback on my potential contribution and if this is a wise feature to change or implement. --001a113cd16e62b7b205127b8892--