1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| $mot = 'А роза упала на лапу Азора';
$result = true;
$time_start = microtime(true);
for ($j=0; $j<1000;$j++)
{
$lettres = mb_ereg_replace(" ", "", mb_strtolower($mot,'UTF-8'));
$longueur = mb_strlen($lettres) / 2;
for($i = 0; $i < $longueur; $i++) {
if (mb_substr($lettres, $i,1,'UTF-8') != mb_substr($lettres, -$i-1 ,1,'UTF-8'))
{
$result = false;
}
}
$reponse = $result === true ? "L'expression '".$mot."' est un palindrome" : "L'expression '".$mot."' n'est pas un palindrome";
}
$time_end = microtime(true);
echo $reponse.'<br />';
echo '$time = '.($time_end - $time_start).'<br />';
//affiche $time = 0.6318371295929
function mb_str_split( $string )
{
return preg_split('/(?<!^)(?!$)/u', $string );
}
$time_start = microtime(true);
for ($j=0; $j<1000;$j++)
{
$lettres = str_replace(" ", "", mb_strtolower($mot,"utf-8"));
$tab_lettres = mb_str_split($lettres);
$reponse = $tab_lettres === array_reverse($tab_lettres) ? "L'expression '".$mot."' est un palindrome" : "L'expression '".$mot."' n'est pas un palindrome";
}
$time_end = microtime(true);
echo $reponse.'<br />';
echo '$time = '.($time_end - $time_start).'<br />';
// affiche $time = 0.056771993637085 |
Partager