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; $time_start = microtime(true); foreach ($countries as $current) { if ($current == $country) { break; } } echo 'Total execution time in seconds: ' . number_format(microtime(true) - $time_start, 20) . PHP_EOL;
Output for 7.3.5
Total execution time in seconds: 0.00005197525024414062 Total execution time in seconds: 0.00000214576721191406 Total execution time in seconds: 0.00000596046447753906
Output for 7.3.4
Total execution time in seconds: 0.00003194808959960938 Total execution time in seconds: 0.00000095367431640625 Total execution time in seconds: 0.00000405311584472656
Output for 7.3.3
Total execution time in seconds: 0.00813817977905273438 Total execution time in seconds: 0.00000500679016113281 Total execution time in seconds: 0.00000500679016113281
Output for 7.3.2
Total execution time in seconds: 0.00008296966552734375 Total execution time in seconds: 0.00000190734863281250 Total execution time in seconds: 0.00001001358032226562
Output for 7.3.1
Total execution time in seconds: 0.00007700920104980469 Total execution time in seconds: 0.00000095367431640625 Total execution time in seconds: 0.00000405311584472656
Output for 7.3.0
Total execution time in seconds: 0.00645804405212402344 Total execution time in seconds: 0.00000405311584472656 Total execution time in seconds: 0.00000500679016113281
Output for 7.2.18
Total execution time in seconds: 0.00006699562072753906 Total execution time in seconds: 0.00000286102294921875 Total execution time in seconds: 0.00000715255737304688
Output for 7.2.17
Total execution time in seconds: 0.00486803054809570312 Total execution time in seconds: 0.00000381469726562500 Total execution time in seconds: 0.00000405311584472656
Output for 7.2.16
Total execution time in seconds: 0.00523400306701660156 Total execution time in seconds: 0.00000405311584472656 Total execution time in seconds: 0.00000500679016113281
Output for 7.2.15
Total execution time in seconds: 0.00010418891906738281 Total execution time in seconds: 0.00000309944152832031 Total execution time in seconds: 0.00000500679016113281
Output for 7.2.14
Total execution time in seconds: 0.00012278556823730469 Total execution time in seconds: 0.00000190734863281250 Total execution time in seconds: 0.00000500679016113281
Output for 7.2.13
Total execution time in seconds: 0.00009894371032714844 Total execution time in seconds: 0.00000190734863281250 Total execution time in seconds: 0.00000500679016113281
Output for 7.2.12
Total execution time in seconds: 0.00012397766113281250 Total execution time in seconds: 0.00000309944152832031 Total execution time in seconds: 0.00000596046447753906
Output for 7.2.11
Total execution time in seconds: 0.00007510185241699219 Total execution time in seconds: 0.00000190734863281250 Total execution time in seconds: 0.00000286102294921875
Output for 7.2.10
Total execution time in seconds: 0.00477600097656250000 Total execution time in seconds: 0.00000405311584472656 Total execution time in seconds: 0.00001001358032226562
Output for 7.2.9
Total execution time in seconds: 0.00313496589660644531 Total execution time in seconds: 0.00000381469726562500 Total execution time in seconds: 0.00000786781311035156
Output for 7.2.8
Total execution time in seconds: 0.00501894950866699219 Total execution time in seconds: 0.00000286102294921875 Total execution time in seconds: 0.00002002716064453125
Output for 7.2.7
Total execution time in seconds: 0.00460696220397949219 Total execution time in seconds: 0.00000405311584472656 Total execution time in seconds: 0.00000381469726562500
Output for 7.2.6
Total execution time in seconds: 0.00005483627319335938 Total execution time in seconds: 0.00000214576721191406 Total execution time in seconds: 0.00000500679016113281
Output for 7.2.5
Total execution time in seconds: 0.00005197525024414062 Total execution time in seconds: 0.00000190734863281250 Total execution time in seconds: 0.00000882148742675781
Output for 7.2.4
Total execution time in seconds: 0.00014090538024902344 Total execution time in seconds: 0.00000691413879394531 Total execution time in seconds: 0.00000977516174316406
Output for 7.2.3
Total execution time in seconds: 0.00005817413330078125 Total execution time in seconds: 0.00000214576721191406 Total execution time in seconds: 0.00000500679016113281
Output for 7.2.2
Total execution time in seconds: 0.00005602836608886719 Total execution time in seconds: 0.00000309944152832031 Total execution time in seconds: 0.00000405311584472656
Output for 7.2.1
Total execution time in seconds: 0.00011205673217773438 Total execution time in seconds: 0.00000309944152832031 Total execution time in seconds: 0.00000691413879394531
Output for 7.2.0
Total execution time in seconds: 0.00005602836608886719 Total execution time in seconds: 0.00000286102294921875 Total execution time in seconds: 0.00000810623168945313
Output for 7.1.28
Total execution time in seconds: 0.00580286979675292969 Total execution time in seconds: 0.00000500679016113281 Total execution time in seconds: 0.00001096725463867188
Output for 7.1.27
Total execution time in seconds: 0.00219011306762695312 Total execution time in seconds: 0.00005316734313964844 Total execution time in seconds: 0.00001001358032226562
Output for 7.1.26
Total execution time in seconds: 0.00004410743713378906 Total execution time in seconds: 0.00000190734863281250 Total execution time in seconds: 0.00000405311584472656
Output for 7.1.25
Total execution time in seconds: 0.00011396408081054688 Total execution time in seconds: 0.00000190734863281250 Total execution time in seconds: 0.00000500679016113281

preferences:
69.18 ms | 401 KiB | 34 Q