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.

Résultat

éxécuté 100000 fois
testchrono secondesratio
'hello'
ratio
time()
ratio
sprintf('a %s', time())
ratio
sprintf('a %s', 'hello')
$a = time();0.063431.868531.000000.314490.41980
$a = sprintf('a %s', time());0.201705.941503.179771.000001.33487
$a = str_replace('%s', time(), 'a %s');0.215586.350463.398641.068831.42675
$a = strtr('a %s', array('%s'=> time() ));0.286588.441904.517931.420841.89663
$a = str_replace('%t', time(), str_replace('%u', time(), str_replace('%s', time(), 'a %s %t %u'));0.5586716.457108.807512.769853.69740
$a = str_replace(array('%t','%u','%s'), array(time(),time(),time(),), 'a %s %t %u');0.6897120.3171210.873313.419534.56463
$a = strtr('a %s %t %u', array('%s'=> time(), '%t'=> time(), '%u'=> time(), ));0.4964314.623837.826372.461303.28552
$a = 'hello';0.033951.000000.535180.168310.22467
$a = sprintf('a %s', 'hello');0.151104.450992.382080.749141.00000
$a = str_replace('%s', 'hello', 'a %s');0.161224.749052.541600.799301.06696
$a = strtr('a %s', array('%s'=> 'hello' ));0.200345.901463.158340.993261.32588
$a = str_replace('%t', 'hello', str_replace('%u', 'hello', str_replace('%s', 'hello', 'a %s %t %u'));0.4081712.023656.434812.023672.70134
$a = str_replace(array('%t','%u','%s'), array('hello','hello','hello',), 'a %s %t %u');0.5182515.266578.170362.569483.42993
$a = strtr('a %s %t %u', array('%s'=> 'hello', '%t'=> 'hello', '%u'=> 'hello', ));0.319299.405615.033691.583032.11315

Lire la suite...