3v4l.org

run code in 200+ 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;
based on OnSd4
Output for 7.3.5
Total execution time in seconds: 0.00003099441528320312 Total execution time in seconds: 0.00000190734863281250
Output for 7.3.4
Total execution time in seconds: 0.00005483627319335938 Total execution time in seconds: 0.00000381469726562500
Output for 7.3.3
Total execution time in seconds: 0.00008296966552734375 Total execution time in seconds: 0.00000596046447753906
Output for 7.3.2
Total execution time in seconds: 0.00007200241088867188 Total execution time in seconds: 0.00000095367431640625
Output for 7.3.1
Total execution time in seconds: 0.00009918212890625000 Total execution time in seconds: 0.00000309944152832031
Output for 7.3.0
Total execution time in seconds: 0.00008201599121093750 Total execution time in seconds: 0.00000190734863281250
Output for 7.2.18
Total execution time in seconds: 0.00014400482177734375 Total execution time in seconds: 0.00000405311584472656
Output for 7.2.17
Total execution time in seconds: 0.00006389617919921875 Total execution time in seconds: 0.00000286102294921875
Output for 7.2.16
Total execution time in seconds: 0.00009703636169433594 Total execution time in seconds: 0.00000309944152832031
Output for 7.2.15
Total execution time in seconds: 0.00010609626770019531 Total execution time in seconds: 0.00000286102294921875
Output for 7.2.14
Total execution time in seconds: 0.00014305114746093750 Total execution time in seconds: 0.00000381469726562500
Output for 7.2.13
Total execution time in seconds: 0.00004100799560546875 Total execution time in seconds: 0.00000214576721191406
Output for 7.2.12
Total execution time in seconds: 0.00004601478576660156 Total execution time in seconds: 0.00000190734863281250
Output for 7.2.11
Total execution time in seconds: 0.00010180473327636719 Total execution time in seconds: 0.00000190734863281250
Output for 7.2.10
Total execution time in seconds: 0.00008392333984375000 Total execution time in seconds: 0.00000214576721191406
Output for 7.2.9
Total execution time in seconds: 0.00005102157592773438 Total execution time in seconds: 0.00000095367431640625
Output for 7.2.8
Total execution time in seconds: 0.00010800361633300781 Total execution time in seconds: 0.00000309944152832031
Output for 7.2.7
Total execution time in seconds: 0.00005483627319335938 Total execution time in seconds: 0.00000309944152832031
Output for 7.2.6
Total execution time in seconds: 0.00007987022399902344 Total execution time in seconds: 0.00000190734863281250
Output for 7.2.5
Total execution time in seconds: 0.00010800361633300781 Total execution time in seconds: 0.00000214576721191406
Output for 7.2.4
Total execution time in seconds: 0.00005006790161132812 Total execution time in seconds: 0.00000190734863281250
Output for 7.2.3
Total execution time in seconds: 0.00010609626770019531 Total execution time in seconds: 0.00000309944152832031
Output for 7.2.2
Total execution time in seconds: 0.00005602836608886719 Total execution time in seconds: 0.00000286102294921875
Output for 7.2.1
Total execution time in seconds: 0.00012397766113281250 Total execution time in seconds: 0.00000190734863281250
Output for 7.2.0
Total execution time in seconds: 0.00010204315185546875 Total execution time in seconds: 0.00000214576721191406
Output for 7.1.28
Total execution time in seconds: 0.00005698204040527344 Total execution time in seconds: 0.00000190734863281250
Output for 7.1.27
Total execution time in seconds: 0.00004100799560546875 Total execution time in seconds: 0.00000190734863281250
Output for 7.1.26
Total execution time in seconds: 0.00015616416931152344 Total execution time in seconds: 0.00000691413879394531
Output for 7.1.25
Total execution time in seconds: 0.00005698204040527344 Total execution time in seconds: 0.00000309944152832031