<?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);
echo array_search($country, $countries);
echo 'Total execution time in seconds: ' . number_format(microtime(true) - $time_start, 20) . PHP_EOL;
- Output for 7.1.6, 7.3.6, 7.4.1
- Total execution time in seconds: 0.00003790855407714844
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.4.0
- Total execution time in seconds: 0.00002503395080566406
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.3.13
- Total execution time in seconds: 0.00004291534423828125
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.3.12
- Total execution time in seconds: 0.00003504753112792969
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.3.11
- Total execution time in seconds: 0.00003886222839355469
174Total execution time in seconds: 0.00000381469726562500
- Output for 7.3.10
- Total execution time in seconds: 0.00002908706665039062
174Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.22, 7.3.9
- Total execution time in seconds: 0.00003194808959960938
174Total execution time in seconds: 0.00000309944152832031
- Output for 7.1.8, 7.1.16, 7.3.8
- Total execution time in seconds: 0.00003695487976074219
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.3.7
- Total execution time in seconds: 0.00004196166992187500
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.3.5
- Total execution time in seconds: 0.00003385543823242188
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.3.4
- Total execution time in seconds: 0.00004887580871582031
174Total execution time in seconds: 0.00000596046447753906
- Output for 7.1.10, 7.3.3
- Total execution time in seconds: 0.00003194808959960938
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.3.2
- Total execution time in seconds: 0.00003314018249511719
174Total execution time in seconds: 0.00000309944152832031
- Output for 7.3.1
- Total execution time in seconds: 0.00003814697265625000
174Total execution time in seconds: 0.00000309944152832031
- Output for 7.1.24, 7.3.0
- Total execution time in seconds: 0.00003504753112792969
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.1.3, 7.2.13, 7.2.26
- Total execution time in seconds: 0.00004601478576660156
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.2.25
- Total execution time in seconds: 0.00004506111145019531
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.2.24
- Total execution time in seconds: 0.00003218650817871094
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.2.6, 7.2.23
- Total execution time in seconds: 0.00003886222839355469
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.2.21
- Total execution time in seconds: 0.00003910064697265625
174Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.20
- Total execution time in seconds: 0.00006484985351562500
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.2.11, 7.2.19
- Total execution time in seconds: 0.00005006790161132812
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.2.18
- Total execution time in seconds: 0.00004506111145019531
174Total execution time in seconds: 0.00000381469726562500
- Output for 7.2.17
- Total execution time in seconds: 0.00003695487976074219
174Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.20, 7.1.25, 7.2.16
- Total execution time in seconds: 0.00003409385681152344
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.2.15
- Total execution time in seconds: 0.00003886222839355469
174Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.0, 7.2.14
- Total execution time in seconds: 0.00004196166992187500
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.2.12
- Total execution time in seconds: 0.00005698204040527344
174Total execution time in seconds: 0.00000905990600585938
- Output for 7.1.12, 7.2.10
- Total execution time in seconds: 0.00005292892456054688
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.1.11, 7.2.9
- Total execution time in seconds: 0.00005102157592773438
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.2.8
- Total execution time in seconds: 0.00005602836608886719
174Total execution time in seconds: 0.00000619888305664063
- Output for 7.2.7
- Total execution time in seconds: 0.00005388259887695312
174Total execution time in seconds: 0.00000596046447753906
- Output for 7.2.5
- Total execution time in seconds: 0.00003409385681152344
174Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.4
- Total execution time in seconds: 0.00003600120544433594
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.2.3
- Total execution time in seconds: 0.00003695487976074219
174Total execution time in seconds: 0.00000309944152832031
- Output for 7.2.2
- Total execution time in seconds: 0.00005197525024414062
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.2.1
- Total execution time in seconds: 0.00003981590270996094
174Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.0
- Total execution time in seconds: 0.00003504753112792969
174Total execution time in seconds: 0.00000309944152832031
- Output for 7.1.33
- Total execution time in seconds: 0.00005197525024414062
174Total execution time in seconds: 0.00000905990600585938
- Output for 7.1.32
- Total execution time in seconds: 0.00002884864807128906
174Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.31
- Total execution time in seconds: 0.00005006790161132812
174Total execution time in seconds: 0.00000596046447753906
- Output for 7.1.30
- Total execution time in seconds: 0.00003004074096679688
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.1.29
- Total execution time in seconds: 0.00002789497375488281
174Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.28
- Total execution time in seconds: 0.00005316734313964844
174Total execution time in seconds: 0.00000882148742675781
- Output for 7.1.23, 7.1.27
- Total execution time in seconds: 0.00003314018249511719
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.1.26
- Total execution time in seconds: 0.00004100799560546875
174Total execution time in seconds: 0.00000691413879394531
- Output for 7.1.22
- Total execution time in seconds: 0.00004005432128906250
174Total execution time in seconds: 0.00000405311584472656
- Output for 7.1.21
- Total execution time in seconds: 0.00003504753112792969
174Total execution time in seconds: 0.00000381469726562500
- Output for 7.1.19
- Total execution time in seconds: 0.00003099441528320312
174Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.18
- Total execution time in seconds: 0.00004196166992187500
174Total execution time in seconds: 0.00000905990600585938
- Output for 7.1.17
- Total execution time in seconds: 0.00004696846008300781
174Total execution time in seconds: 0.00000596046447753906
- Output for 7.1.15
- Total execution time in seconds: 0.00004506111145019531
174Total execution time in seconds: 0.00000691413879394531
- Output for 7.1.7, 7.1.14
- Total execution time in seconds: 0.00004005432128906250
174Total execution time in seconds: 0.00000596046447753906
- Output for 7.1.13
- Total execution time in seconds: 0.00005722045898437500
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.1.9
- Total execution time in seconds: 0.00005912780761718750
174Total execution time in seconds: 0.00000691413879394531
- Output for 7.1.5
- Total execution time in seconds: 0.00003910064697265625
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.1.4
- Total execution time in seconds: 0.00003218650817871094
174Total execution time in seconds: 0.00000309944152832031
- Output for 7.1.2
- Total execution time in seconds: 0.00004696846008300781
174Total execution time in seconds: 0.00000500679016113281
- Output for 7.1.1
- Total execution time in seconds: 0.00003814697265625000
174Total execution time in seconds: 0.00000381469726562500
preferences:
104.59 ms | 401 KiB | 82 Q