[TYPO3-core] RFC Feature #16189: Optimize stdWrap usage for TypoScript content element CLEARGIF

Fabrizio Branca news at _REMOVETHIS_fabrizio-branca.de
Mon Nov 1 09:13:32 CET 2010


Hi Joey,

On 01.11.2010 00:14, JoH asenau wrote:
> So are there any reliable sources for benchmarks of sprintf and ternary operators vs. if/else?

I checked this with

<?php
$starttime = microtime(true);
$a = 'quick'; $b = 'fox'; $c = 'over'; $d = 'dog';
$iterations = 1000000;
for ($i=0; $i<$iterations;$i++) {
	// $string = 'The ' . $a . ' brown ' . $b . ' jumps ' . $c . ' the lazy 
' . $dog;
	$string =  sprintf('The %s brown %s jumps %s the lazy %s', $a, $b, $c, $d);
}
$duration = microtime(true) - $starttime;
$duration *= 1000 * 1000;
echo round($duration/$iterations, 2) . ' microsec/call<br />';


Results:
sprintf takes 5.63 _micro_seconds per call on my machine
string concatenation takes 2.94 _micro_seconds per call

So, isn't it worth to additionally spend 2,96 _micro_seconds for a much 
more readable code?
(Just for comparison: Save a single database request somewhere else and 
you easily save _1000x_ more time...)

I use sprintf also for exception messages and other places where I have 
to insert dynamic content into a static string. It is somehow like a 
little "templating engine" and it gives you the possibility to easily 
search for that string in the code. Additionally you can see at first 
sight how the resulting string will look like and can check if all 
brackets and quotes are in place.

Maybe this is peanuts for this patch, but I would really like to see 
this in other parts of the core as-well...

Bye,

Fabrizio


More information about the TYPO3-team-core mailing list