<?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;
- Output for 7.4.1
- Total execution time in seconds: 0.00004506111145019531
Total execution time in seconds: 0.00000405311584472656
- Output for 7.4.0
- Total execution time in seconds: 0.00004100799560546875
Total execution time in seconds: 0.00000190734863281250
- Output for 7.3.13
- Total execution time in seconds: 0.00004792213439941406
Total execution time in seconds: 0.00000286102294921875
- Output for 7.3.12
- Total execution time in seconds: 0.00005197525024414062
Total execution time in seconds: 0.00000309944152832031
- Output for 7.3.11
- Total execution time in seconds: 0.00005912780761718750
Total execution time in seconds: 0.00000309944152832031
- Output for 7.3.10
- Total execution time in seconds: 0.00006103515625000000
Total execution time in seconds: 0.00000405311584472656
- Output for 7.3.9
- Total execution time in seconds: 0.00002884864807128906
Total execution time in seconds: 0.00000095367431640625
- Output for 7.3.8
- Total execution time in seconds: 0.00003695487976074219
Total execution time in seconds: 0.00000095367431640625
- Output for 7.3.7
- Total execution time in seconds: 0.00003886222839355469
Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.15, 7.3.6
- Total execution time in seconds: 0.00003409385681152344
Total execution time in seconds: 0.00000095367431640625
- Output for 7.3.5
- Total execution time in seconds: 0.00003409385681152344
Total execution time in seconds: 0.00000190734863281250
- Output for 7.3.4
- Total execution time in seconds: 0.00004410743713378906
Total execution time in seconds: 0.00000190734863281250
- Output for 7.1.9, 7.1.11, 7.3.3
- Total execution time in seconds: 0.00003004074096679688
Total execution time in seconds: 0.00000214576721191406
- Output for 7.1.28, 7.3.2
- Total execution time in seconds: 0.00004696846008300781
Total execution time in seconds: 0.00000286102294921875
- Output for 7.3.1
- Total execution time in seconds: 0.00003504753112792969
Total execution time in seconds: 0.00000286102294921875
- Output for 7.3.0
- Total execution time in seconds: 0.00002908706665039062
Total execution time in seconds: 0.00000214576721191406
- Output for 7.2.26
- Total execution time in seconds: 0.00005197525024414062
Total execution time in seconds: 0.00000405311584472656
- Output for 7.1.14, 7.2.25
- Total execution time in seconds: 0.00004100799560546875
Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.24
- Total execution time in seconds: 0.00006389617919921875
Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.16, 7.2.17, 7.2.23
- Total execution time in seconds: 0.00003695487976074219
Total execution time in seconds: 0.00000190734863281250
- Output for 7.2.22
- Total execution time in seconds: 0.00004506111145019531
Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.21
- Total execution time in seconds: 0.00005912780761718750
Total execution time in seconds: 0.00000381469726562500
- Output for 7.2.20
- Total execution time in seconds: 0.00004196166992187500
Total execution time in seconds: 0.00000214576721191406
- Output for 7.2.19
- Total execution time in seconds: 0.00003814697265625000
Total execution time in seconds: 0.00000214576721191406
- Output for 7.2.18
- Total execution time in seconds: 0.00005078315734863281
Total execution time in seconds: 0.00000309944152832031
- Output for 7.2.16
- Total execution time in seconds: 0.00003600120544433594
Total execution time in seconds: 0.00000190734863281250
- Output for 7.2.14
- Total execution time in seconds: 0.00003695487976074219
Total execution time in seconds: 0.00000214576721191406
- Output for 7.2.13
- Total execution time in seconds: 0.00004982948303222656
Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.6, 7.2.12
- Total execution time in seconds: 0.00003504753112792969
Total execution time in seconds: 0.00000214576721191406
- Output for 7.2.11
- Total execution time in seconds: 0.00003385543823242188
Total execution time in seconds: 0.00000190734863281250
- Output for 7.2.10
- Total execution time in seconds: 0.00005006790161132812
Total execution time in seconds: 0.00000190734863281250
- Output for 7.2.9
- Total execution time in seconds: 0.00003790855407714844
Total execution time in seconds: 0.00000119209289550781
- Output for 7.2.8
- Total execution time in seconds: 0.00004601478576660156
Total execution time in seconds: 0.00000214576721191406
- Output for 7.2.7
- Total execution time in seconds: 0.00003385543823242188
Total execution time in seconds: 0.00000095367431640625
- Output for 7.2.5
- Total execution time in seconds: 0.00003600120544433594
Total execution time in seconds: 0.00000095367431640625
- Output for 7.2.4
- Total execution time in seconds: 0.00004220008850097656
Total execution time in seconds: 0.00000405311584472656
- Output for 7.2.3
- Total execution time in seconds: 0.00005006790161132812
Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.2
- Total execution time in seconds: 0.00005292892456054688
Total execution time in seconds: 0.00000286102294921875
- Output for 7.2.1
- Total execution time in seconds: 0.00004386901855468750
Total execution time in seconds: 0.00000214576721191406
- Output for 7.2.0
- Total execution time in seconds: 0.00004100799560546875
Total execution time in seconds: 0.00000309944152832031
- Output for 7.1.33
- Total execution time in seconds: 0.00006008148193359375
Total execution time in seconds: 0.00000596046447753906
- Output for 7.1.2, 7.1.4, 7.1.32
- Total execution time in seconds: 0.00003910064697265625
Total execution time in seconds: 0.00000190734863281250
- Output for 7.1.5, 7.1.31
- Total execution time in seconds: 0.00003290176391601562
Total execution time in seconds: 0.00000190734863281250
- Output for 7.1.30
- Total execution time in seconds: 0.00003790855407714844
Total execution time in seconds: 0.00000190734863281250
- Output for 7.1.26, 7.1.29
- Total execution time in seconds: 0.00003695487976074219
Total execution time in seconds: 0.00000309944152832031
- Output for 7.1.27
- Total execution time in seconds: 0.00005292892456054688
Total execution time in seconds: 0.00000095367431640625
- Output for 7.1.25
- Total execution time in seconds: 0.00004506111145019531
Total execution time in seconds: 0.00000309944152832031
- Output for 7.1.24
- Total execution time in seconds: 0.00003504753112792969
Total execution time in seconds: 0.00000190734863281250
- Output for 7.1.23
- Total execution time in seconds: 0.00004291534423828125
Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.22
- Total execution time in seconds: 0.00003194808959960938
Total execution time in seconds: 0.00000214576721191406
- Output for 7.1.21
- Total execution time in seconds: 0.00005006790161132812
Total execution time in seconds: 0.00000405311584472656
- Output for 7.1.20
- Total execution time in seconds: 0.00003814697265625000
Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.19
- Total execution time in seconds: 0.00003910064697265625
Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.18
- Total execution time in seconds: 0.00004887580871582031
Total execution time in seconds: 0.00000405311584472656
- Output for 7.1.17
- Total execution time in seconds: 0.00003099441528320312
Total execution time in seconds: 0.00000190734863281250
- Output for 7.1.15
- Total execution time in seconds: 0.00003004074096679688
Total execution time in seconds: 0.00000095367431640625
- Output for 7.1.13
- Total execution time in seconds: 0.00002789497375488281
Total execution time in seconds: 0.00000095367431640625
- Output for 7.1.12
- Total execution time in seconds: 0.00002884864807128906
Total execution time in seconds: 0.00000214576721191406
- Output for 7.1.10
- Total execution time in seconds: 0.00004005432128906250
Total execution time in seconds: 0.00000309944152832031
- Output for 7.1.8
- Total execution time in seconds: 0.00005412101745605469
Total execution time in seconds: 0.00000405311584472656
- Output for 7.1.7
- Total execution time in seconds: 0.00004005432128906250
Total execution time in seconds: 0.00000286102294921875
- Output for 7.1.6
- Total execution time in seconds: 0.00004696846008300781
Total execution time in seconds: 0.00000405311584472656
- Output for 7.1.3
- Total execution time in seconds: 0.00002694129943847656
Total execution time in seconds: 0.00000190734863281250
- Output for 7.1.1
- Total execution time in seconds: 0.00003314018249511719
Total execution time in seconds: 0.00000309944152832031
- Output for 7.1.0
- Total execution time in seconds: 0.00003194808959960938
Total execution time in seconds: 0.00000309944152832031
preferences:
83.78 ms | 401 KiB | 82 Q