mer
08
avr '09
benchmark de php pour sprintf, strtr et str_replace
À 15:39 dans la rubrique PHP / PEAR
←
/ #929
/ rss
/ →
J'ai testé
sprintf,
strtr
et
str_replace.
100K passages sur chaque test.
Une version avec time(), une autre juste hello.
J'ai pris 4 références de comparaison pour faire le tableau. La colonne qui m'intéresse le plus est la référence 4. où le sprintf sur hello est 1.
Une version avec time(), une autre juste hello.
J'ai pris 4 références de comparaison pour faire le tableau. La colonne qui m'intéresse le plus est la référence 4. où le sprintf sur hello est 1.
Résultat
éxécuté 100000 fois| test | chrono secondes | ratio 'hello' | ratio time() | ratio sprintf('a %s', time()) | ratio sprintf('a %s', 'hello') |
|---|---|---|---|---|---|
| $a = time(); | 0.06343 | 1.86853 | 1.00000 | 0.31449 | 0.41980 |
| $a = sprintf('a %s', time()); | 0.20170 | 5.94150 | 3.17977 | 1.00000 | 1.33487 |
| $a = str_replace('%s', time(), 'a %s'); | 0.21558 | 6.35046 | 3.39864 | 1.06883 | 1.42675 |
| $a = strtr('a %s', array('%s'=> time() )); | 0.28658 | 8.44190 | 4.51793 | 1.42084 | 1.89663 |
| $a = str_replace('%t', time(), str_replace('%u', time(), str_replace('%s', time(), 'a %s %t %u')); | 0.55867 | 16.45710 | 8.80751 | 2.76985 | 3.69740 |
| $a = str_replace(array('%t','%u','%s'), array(time(),time(),time(),), 'a %s %t %u'); | 0.68971 | 20.31712 | 10.87331 | 3.41953 | 4.56463 |
| $a = strtr('a %s %t %u', array('%s'=> time(), '%t'=> time(), '%u'=> time(), )); | 0.49643 | 14.62383 | 7.82637 | 2.46130 | 3.28552 |
| $a = 'hello'; | 0.03395 | 1.00000 | 0.53518 | 0.16831 | 0.22467 |
| $a = sprintf('a %s', 'hello'); | 0.15110 | 4.45099 | 2.38208 | 0.74914 | 1.00000 |
| $a = str_replace('%s', 'hello', 'a %s'); | 0.16122 | 4.74905 | 2.54160 | 0.79930 | 1.06696 |
| $a = strtr('a %s', array('%s'=> 'hello' )); | 0.20034 | 5.90146 | 3.15834 | 0.99326 | 1.32588 |
| $a = str_replace('%t', 'hello', str_replace('%u', 'hello', str_replace('%s', 'hello', 'a %s %t %u')); | 0.40817 | 12.02365 | 6.43481 | 2.02367 | 2.70134 |
| $a = str_replace(array('%t','%u','%s'), array('hello','hello','hello',), 'a %s %t %u'); | 0.51825 | 15.26657 | 8.17036 | 2.56948 | 3.42993 |
| $a = strtr('a %s %t %u', array('%s'=> 'hello', '%t'=> 'hello', '%u'=> 'hello', )); | 0.31929 | 9.40561 | 5.03369 | 1.58303 | 2.11315 |
[PHP]
<?php
$timeList = array();
$testTitle = array();
$testTitle [] = "\$a = time();";
$nb_occur=100000;
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = time();
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$refTime2=$time;
$testTitle [] = "\$a = sprintf('a %s', time());";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = sprintf('a %s', time());
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$refTime3=$time;
$timeList[]=$time;
$testTitle [] = "\$a = str_replace('%s', time(), 'a %s');";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = str_replace('%s', time(), 'a %s');
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$testTitle [] = "\$a = strtr('a %s', array('%s'=> time() ));";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = strtr('a %s', array('%s'=> time() ));
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$testTitle [] = "\$a = str_replace('%t', time(), str_replace('%u', time(), str_replace('%s', time(), 'a %s %t %u'));";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = str_replace('%t', time(), str_replace('%u', time(), str_replace('%s', time(), 'a %s %t %u')));
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$testTitle [] = "\$a = str_replace(array('%t','%u','%s'), array(time(),time(),time(),), 'a %s %t %u');";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = str_replace(array('%t','%u','%s'), array(time(),time(),time(),), 'a %s %t %u');
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$testTitle [] = "\$a = strtr('a %s %t %u', array('%s'=> time(), '%t'=> time(), '%u'=> time(), ));";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = strtr('a %s %t %u', array('%s'=> time(), '%t'=> time(), '%u'=> time(), ));
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$testTitle [] = "\$a = 'hello';";
$nb_occur=100000;
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = 'hello';
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$refTime1=$time;
$timeList[]=$time;
$testTitle [] = "\$a = sprintf('a %s', 'hello');";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = sprintf('a %s', 'hello');
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$refTime4=$time;
$timeList[]=$time;
$testTitle [] = "\$a = str_replace('%s', 'hello', 'a %s');";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = str_replace('%s', 'hello', 'a %s');
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$testTitle [] = "\$a = strtr('a %s', array('%s'=> 'hello' ));";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = strtr('a %s', array('%s'=> 'hello' ));
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$testTitle [] = "\$a = str_replace('%t', 'hello', str_replace('%u', 'hello', str_replace('%s', 'hello', 'a %s %t %u'));";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = str_replace('%t', 'hello', str_replace('%u', 'hello', str_replace('%s', 'hello', 'a %s %t %u')));
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$testTitle [] = "\$a = str_replace(array('%t','%u','%s'), array('hello','hello','hello',), 'a %s %t %u');";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = str_replace(array('%t','%u','%s'), array('hello','hello','hello',), 'a %s %t %u');
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
$testTitle [] = "\$a = strtr('a %s %t %u', array('%s'=> 'hello', '%t'=> 'hello', '%u'=> 'hello', ));";
$time_start = microtime(true);
for ($i=0 ; $i<$nb_occur; $i++)
{
$a = strtr('a %s %t %u', array('%s'=> 'hello', '%t'=> 'hello', '%u'=> 'hello', ));
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$timeList[]=$time;
echo 'éxécuté '.$nb_occur.' fois <table border=1><tr><th>test<th>chrono secondes<th>ratio<br/>\'hello\'<th>ratio<br/>time()<th>ratio<br />sprintf(\'a %s\', time())<th>ratio<br/>sprintf(\'a %s\', \'hello\')</tr>';
foreach($timeList as $k=>$chrono)
{
echo '<tr><td>' . $testTitle [$k]
. '<td>'
. sprintf('%01.5f',$chrono)
. '<td />'
. sprintf('%01.5f',$chrono/$refTime1)
. '<td />'
. sprintf('%01.5f',$chrono/$refTime2)
. '<td />'
. sprintf('%01.5f',$chrono/$refTime3)
. '<td />'
. sprintf('%01.5f',$chrono/$refTime4)
;
}
echo '</table>';
?>








Commentaires
Aucun commentaire pour le moment.
Ajouter un commentaire