3v4l.org

run code in 300+ PHP versions simultaneously
<?php function BinSearch($needle, $haystack) { $m = ceil(count($haystack) / 2); $z = $haystack[$m]; if ($needle == $haystack[$m]) { return "Found at index: $haystack[$needle]"; $upper = array_slice($haystack, -$m, count($haystack)); return BinSearch($needle, $upper); } else if ($needle < $haystack[$m]) { $lower = array_slice($haystack, 0, $m); return BinSearch($needle, $lower); } } $countries = array("Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Barbuda", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Trty.", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Caicos Islands", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Futuna Islands", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard", "Herzegovina", "Holy See", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Jan Mayen Islands", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea", "Korea (Democratic)", "Kuwait", "Kyrgyzstan", "Lao", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "McDonald Islands", "Mexico", "Micronesia", "Miquelon", "Moldova", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "Nevis", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Palestinian Territory, Occupied", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Principe", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Barthelemy", "Saint Helena", "Saint Kitts", "Saint Lucia", "Saint Martin (French part)", "Saint Pierre", "Saint Vincent", "Samoa", "San Marino", "Sao Tome", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia", "South Sandwich Islands", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Grenadines", "Timor-Leste", "Tobago", "Togo", "Tokelau", "Tonga", "Trinidad", "Tunisia", "Turkey", "Turkmenistan", "Turks Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "US Minor Outlying Islands", "Uzbekistan", "Vanuatu", "Vatican City State", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (US)", "Wallis", "Western Sahara", "Yemen", "Zambia", "Zimbabwe"); $country = "Pakistan"; $time_start = microtime(true); echo BinSearch($country, $countries) . PHP_EOL; echo 'Total execution time in seconds: ' . number_format(microtime(true) - $time_start, 20) . PHP_EOL; $time_start = microtime(true); array_search($country, $countries); echo 'Total execution time in seconds: ' . number_format(microtime(true) - $time_start, 20) . PHP_EOL;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/idoX2
function name:  (null)
number of ops:  43
compiled vars:  !0 = $countries, !1 = $country, !2 = $time_start
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   ASSIGN                                                   !0, <array>
   19     1        ASSIGN                                                   !1, 'Pakistan'
   21     2        INIT_FCALL                                               'microtime'
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $5      
          5        ASSIGN                                                   !2, $5
   22     6        INIT_FCALL                                               'binsearch'
          7        SEND_VAR                                                 !1
          8        SEND_VAR                                                 !0
          9        DO_FCALL                                      0  $7      
         10        CONCAT                                           ~8      $7, '%0A'
         11        ECHO                                                     ~8
   23    12        INIT_FCALL                                               'number_format'
         13        INIT_FCALL                                               'microtime'
         14        SEND_VAL                                                 <true>
         15        DO_ICALL                                         $9      
         16        SUB                                              ~10     $9, !2
         17        SEND_VAL                                                 ~10
         18        SEND_VAL                                                 20
         19        DO_ICALL                                         $11     
         20        CONCAT                                           ~12     'Total+execution+time+in+seconds%3A+', $11
         21        CONCAT                                           ~13     ~12, '%0A'
         22        ECHO                                                     ~13
   25    23        INIT_FCALL                                               'microtime'
         24        SEND_VAL                                                 <true>
         25        DO_ICALL                                         $14     
         26        ASSIGN                                                   !2, $14
   26    27        INIT_FCALL                                               'array_search'
         28        SEND_VAR                                                 !1
         29        SEND_VAR                                                 !0
         30        DO_ICALL                                                 
   27    31        INIT_FCALL                                               'number_format'
         32        INIT_FCALL                                               'microtime'
         33        SEND_VAL                                                 <true>
         34        DO_ICALL                                         $17     
         35        SUB                                              ~18     $17, !2
         36        SEND_VAL                                                 ~18
         37        SEND_VAL                                                 20
         38        DO_ICALL                                         $19     
         39        CONCAT                                           ~20     'Total+execution+time+in+seconds%3A+', $19
         40        CONCAT                                           ~21     ~20, '%0A'
         41        ECHO                                                     ~21
   28    42      > RETURN                                                   1

Function binsearch:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 31
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 45
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/idoX2
function name:  BinSearch
number of ops:  46
compiled vars:  !0 = $needle, !1 = $haystack, !2 = $m, !3 = $z, !4 = $upper, !5 = $lower
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        INIT_FCALL                                               'ceil'
          3        COUNT                                            ~6      !1
          4        DIV                                              ~7      ~6, 2
          5        SEND_VAL                                                 ~7
          6        DO_ICALL                                         $8      
          7        ASSIGN                                                   !2, $8
    6     8        FETCH_DIM_R                                      ~10     !1, !2
          9        ASSIGN                                                   !3, ~10
    8    10        FETCH_DIM_R                                      ~12     !1, !2
         11        IS_EQUAL                                                 !0, ~12
         12      > JMPZ                                                     ~13, ->31
    9    13    >   NOP                                                      
         14        FETCH_DIM_R                                      ~14     !1, !0
         15        FAST_CONCAT                                      ~15     'Found+at+index%3A+', ~14
         16      > RETURN                                                   ~15
   10    17*       INIT_FCALL                                               'array_slice'
         18*       SEND_VAR                                                 !1
         19*       MUL                                              ~16     !2, -1
         20*       SEND_VAL                                                 ~16
         21*       COUNT                                            ~17     !1
         22*       SEND_VAL                                                 ~17
         23*       DO_ICALL                                         $18     
         24*       ASSIGN                                                   !4, $18
   11    25*       INIT_FCALL_BY_NAME                                       'BinSearch'
         26*       SEND_VAR_EX                                              !0
         27*       SEND_VAR_EX                                              !4
         28*       DO_FCALL                                      0  $20     
         29*       RETURN                                                   $20
         30*       JMP                                                      ->45
   12    31    >   FETCH_DIM_R                                      ~21     !1, !2
         32        IS_SMALLER                                               !0, ~21
         33      > JMPZ                                                     ~22, ->45
   13    34    >   INIT_FCALL                                               'array_slice'
         35        SEND_VAR                                                 !1
         36        SEND_VAL                                                 0
         37        SEND_VAR                                                 !2
         38        DO_ICALL                                         $23     
         39        ASSIGN                                                   !5, $23
   14    40        INIT_FCALL_BY_NAME                                       'BinSearch'
         41        SEND_VAR_EX                                              !0
         42        SEND_VAR_EX                                              !5
         43        DO_FCALL                                      0  $25     
         44      > RETURN                                                   $25
   17    45    > > RETURN                                                   null

End of function binsearch

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.73 ms | 1398 KiB | 24 Q