Last active
September 8, 2015 10:35
-
-
Save JesusTheHun/47a626dd94db9bdda5e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$list = " | |
Afghanistan | |
Albania | |
Algeria | |
Andorra | |
Angola | |
Antigua and Barbuda | |
Argentina | |
Armenia | |
Aruba | |
Australia | |
Austria | |
Azerbaijan | |
Cambodia | |
Cameroon | |
Canada | |
Cape Verde | |
Central African Republic | |
Chad | |
Chile | |
China | |
Colombia | |
Comoros | |
Congo, Democratic Republic of the | |
Congo, Republic of the | |
Costa Rica | |
Cote d'Ivoire | |
Croatia | |
Cuba | |
Curacao | |
Cyprus | |
Czech Republic | |
Bahamas, The | |
Bahrain | |
Bangladesh | |
Barbados | |
Belarus | |
Belgium | |
Belize | |
Benin | |
Bhutan | |
Bolivia | |
Bosnia and Herzegovina | |
Botswana | |
Brazil | |
Brunei | |
Bulgaria | |
Burkina Faso | |
Burma | |
Burundi | |
Gabon | |
Gambia, The | |
Georgia | |
Germany | |
Ghana | |
Greece | |
Grenada | |
Guatemala | |
Guinea | |
Guinea-Bissau | |
Guyana | |
Haiti | |
Holy See | |
Honduras | |
Hong Kong | |
Hungary | |
Iceland | |
India | |
Indonesia | |
Iran | |
Iraq | |
Ireland | |
Israel | |
Italy | |
Jamaica | |
Japan | |
Jordan | |
Kazakhstan | |
Kenya | |
Kiribati | |
Korea, North | |
Korea, South | |
Kosovo | |
Kuwait | |
Kyrgyzstan | |
Laos | |
Latvia | |
Lebanon | |
Lesotho | |
Liberia | |
Libya | |
Liechtenstein | |
Lithuania | |
Luxembourg | |
Macau | |
Macedonia | |
Madagascar | |
Malawi | |
Malaysia | |
Maldives | |
Mali | |
Malta | |
Marshall Islands | |
Mauritania | |
Mauritius | |
Mexico | |
Micronesia | |
Moldova | |
Monaco | |
Mongolia | |
Montenegro | |
Morocco | |
Mozambique | |
Namibia | |
Nauru | |
Nepal | |
Netherlands | |
Netherlands Antilles | |
New Zealand | |
Nicaragua | |
Niger | |
Nigeria | |
North Korea | |
Norway | |
Pakistan | |
Palau | |
Palestinian Territories | |
Panama | |
Papua New Guinea | |
Paraguay | |
Peru | |
Philippines | |
Poland | |
Portugal | |
Saint Kitts and Nevis | |
Saint Lucia | |
Saint Vincent and the Grenadines | |
Samoa | |
San Marino | |
Sao Tome and Principe | |
Saudi Arabia | |
Senegal | |
Serbia | |
Seychelles | |
Sierra Leone | |
Singapore | |
Sint Maarten | |
Slovakia | |
Slovenia | |
Solomon Islands | |
Somalia | |
South Africa | |
South Korea | |
South Sudan | |
Spain | |
Sri Lanka | |
Sudan | |
Suriname | |
Swaziland | |
Sweden | |
Switzerland | |
Syria | |
Taiwan | |
Tajikistan | |
Tanzania | |
Thailand | |
Timor-Leste | |
Togo | |
Tonga | |
Trinidad and Tobago | |
Tunisia | |
Turkey | |
Turkmenistan | |
Tuvalu | |
Uganda | |
Ukraine | |
United Arab Emirates | |
United Kingdom | |
Uruguay | |
Uzbekistan | |
Vanuatu | |
Venezuela | |
Vietnam | |
Yemen | |
Zambia | |
Zimbabwe | |
"; | |
$a = explode("\n", trim($list)); | |
$userInput = 'king'; | |
set_time_limit(0); | |
function damn($a) | |
{ | |
global $b; | |
return stripos($a, $b) === false; | |
} | |
$start = microtime(true); | |
for ($i = 0; $i < 1000000; $i++) { | |
$z = array_filter($a, function ($country) use ($userInput) { | |
return stripos($country, $userInput) !== false; | |
}); | |
} | |
$end = microtime(true); | |
echo "array_filter lambda : "; | |
echo $end - $start; | |
echo "<br />"; | |
$start = microtime(true); | |
for ($i = 0; $i < 1000000; $i++) { | |
$z = array_filter($a, 'damn'); | |
} | |
$end = microtime(true); | |
echo "array_filter named : "; | |
echo $end - $start; | |
echo "<br />"; | |
$start = microtime(true); | |
for ($i = 0; $i < 1000000; $i++) { | |
foreach ($a as $country) { | |
if (stripos($country, $userInput) !== false) { | |
$z[] = $country; | |
} | |
} | |
} | |
$end = microtime(true); | |
echo "loop : "; | |
echo $end - $start; | |
echo "<br />"; | |
$start = microtime(true); | |
for ($i = 0; $i < 1000000; $i++) { | |
$z = preg_grep("#".preg_quote($userInput, '#')."#i", $a); | |
} | |
$end = microtime(true); | |
echo "preg_grep : "; | |
echo $end - $start; | |
echo "<br />"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array_filter lambda : 65.047384023666 | |
array_filter named : 76.261443138123 | |
loop : 35.168543815613 | |
preg_grep : 8.8749940395355 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment