mercredi 8 avril 2009
mer
08
avr '09
benchmark de php pour sprintf, strtr et str_replace
Dans la rubrique PHP / PEAR
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 |


-
-





