[Typo3-dev] char <-> string -> overhead -> speed

Michael Johnston mjohnston at planetactive.com
Sun Sep 26 00:38:26 CEST 2004


Unfortunately, there is only one way to optimize effectively. That is 
to profile production code & find the slow parts then use theory, 
intuition, but mostly trial and error to make the slow parts less slow.

Tests like the one you wrote are far too simplistic to be useful at 
all. Want proof? Run it this way:

<?php
$imax=5000000;
$tStart=time();
$nl = "\n";
$bob = 'mary';
for($i=0;$i<$imax;$i++) 
$stringString="TextTextTextText\nnewLine$bob$bob";
$endTime=time()-$tStart;
echo "string=$endTime.<br>";
$tStart=time();
for($i=0;$i<$imax;$i++) 
$stringChar='TextTextTextText'.$nl.'newLine'.$bob.$bob;
$endTime=time()-$tStart;
echo "chr=$endTime.<br>";
?>

....oops! now string takes longer. Why? probably because it's 
interpolation of variables that is time consuming, not interpolation of 
escaped characters.

If you're going to write tests to get an idea of how to pre-optimize 
your code, you'd better write lots of different tests, or you may 
accidentally come up with such a misleading result that you end up 
DE-optimizing your code.

In our case, on a site with many custom content elements & plugins, we 
quickly changed a bunch of interpolated strings that were in loops to 
be non-interpolated and got a nice increase in speed. As measured by 
profiling the actual site on the actual machine, before and after.

Cheers,
Michael Johnston
Planetactive


On 24-Sep-04, at 9:36 PM, Peter Russ wrote:

> <?php
> $stringChar='Test\nnewLine'; // 1
> $stringString="Text\nnewLine"; // 2
>
> //How does the printout look like?
> echo nl2br($stringChar)."<br>".nl2br($stringString);
> ?>
>
> What is the benefit of $stringChar compared to stringString?
> I'm just wondering because to get similar results, either $stringChar 
> has to be defined as
> $stringChar='Test'.char(10).'newLine'; // 3
> or some high performing function as explode or strreplace or ereg has 
> to be involved.UuuOps.
> This might be an overhead.
>
> So
> <?php
> $imax=5000000;
> $tStart=time();
> for($i=0;$i<$imax;$i++) $stringString="Text\nnewLine";
> $endTime=time()-$tStart;
> echo "string=$endTime.<br>";
> $tStart=time();
> for($i=0;$i<$imax;$i++) $stringChar='Test'.chr(10).'newLine';
> $endTime=time()-$tStart;
> echo "chr=$endTime.<br>";
> ?>
>
> string is about 50% more efficient.
>
> Any idea why to stay with $stringChar?
>
> Regs. Peter.
>






More information about the TYPO3-dev mailing list