Raisch
extraordinary bit
Hallo Tutorianer,
hab mir mal einen Benchmark für Stringquotes geschrieben. Um der Frage auf den Grund zu gehen, ob Double- oder Singlequotes schneller sind.
Hier der Benchmark (muss nicht schön aussehen, soll nur seinen Zweck erfüllen!^^):
Das Ergebnis auf meinem Krepelserver mit PHP 5.3.8:
Wenn ihr den Benchmark bei euch ausführt, dann wäre es nett, wenn ihr das Ergebnis auch posten könntet.
Gruß
hab mir mal einen Benchmark für Stringquotes geschrieben. Um der Frage auf den Grund zu gehen, ob Double- oder Singlequotes schneller sind.
Hier der Benchmark (muss nicht schön aussehen, soll nur seinen Zweck erfüllen!^^):
PHP:
<?php
ini_set( 'max_execution_time', 0 );
$intRounds = 100;
$intAllocations = 100000;
$arrLoadTime['SQ'] = 0;
$arrLoadTime['DQ'] = 0;
$arrLoadTime['SQ_ExcludedVar'] = 0;
$arrLoadTime['DQ_ExcludedVar'] = 0;
$arrLoadTime['DQ_IncludedVar'] = 0;
$strTemp = '';
$strStatic = 'Test';
function getEndMicrotimeRounded( $dblStartTime, $intDecimalPlaces )
{
return round( microtime( true ) - $dblStartTime, $intDecimalPlaces );
}
function getFullTimePercentRounded( $dblTimeLow, $dblTimeHigh )
{
return round( ( 100 / $dblTimeLow ) * $dblTimeHigh, 2 );
}
for ( $intRoundsCounter = 0; $intRoundsCounter < $intRounds; ++$intRoundsCounter )
{
$dblStartTime = microtime( true );
for ( $i = 0; $i < $intAllocations; ++$i )
{
$strTemp = 'Hallo Welt, ich bin ein String!';
}
$arrLoadTime['SQ'] += getEndMicrotimeRounded( $dblStartTime, 8 );
$dblStartTime = microtime( true );
for ( $i = 0; $i < $intAllocations; ++$i )
{
$strTemp = "Hallo Welt, ich bin ein String!";
}
$arrLoadTime['DQ'] += getEndMicrotimeRounded( $dblStartTime, 8 );
$dblStartTime = microtime( true );
for ( $i = 0; $i < $intAllocations; ++$i )
{
$strTemp = 'Hallo Welt, ' . $strStatic . 'ich bin ein String!';
}
$arrLoadTime['SQ_ExcludedVar'] += getEndMicrotimeRounded( $dblStartTime, 8 );
$dblStartTime = microtime( true );
for ( $i = 0; $i < $intAllocations; ++$i )
{
$strTemp = "Hallo Welt, " . $strStatic . "ich bin ein String!";
}
$arrLoadTime['DQ_ExcludedVar'] += getEndMicrotimeRounded( $dblStartTime, 8 );
$dblStartTime = microtime( true );
for ( $i = 0; $i < $intAllocations; ++$i )
{
$strTemp = "Hallo Welt, {$strStatic}, ich bin ein String!";
}
$arrLoadTime['DQ_IncludedVar'] += getEndMicrotimeRounded( $dblStartTime, 8 );
}
$arrLoadTime = array_map(
function ( $dblArrayMember ) use ( $intRounds )
{
return $dblArrayMember / $intRounds;
},
$arrLoadTime
);
$intLength = call_user_func(
function ( array $arrLoadTime )
{
$intReturn = 0;
foreach ( $arrLoadTime AS $dblValue )
{
$intTempLength = strlen( '' . $dblValue );
if ( $intTempLength > $intReturn )
{
$intReturn = $intTempLength;
}
}
return $intReturn;
},
$arrLoadTime
);
if ( $arrLoadTime['SQ'] > $arrLoadTime['DQ'] )
{
$arrPercent['SQ'] = getFullTimePercentRounded(
$arrLoadTime['DQ'],
$arrLoadTime['SQ']
);
$arrPercent['DQ'] = '100.00';
}
elseif ( $arrLoadTime['SQ'] < $arrLoadTime['DQ'] )
{
$arrPercent['SQ'] = '100.00';
$arrPercent['DQ'] = getFullTimePercentRounded(
$arrLoadTime['SQ'],
$arrLoadTime['DQ']
);
}
else
{
$arrPercent['SQ'] = '100.00';
$arrPercent['DQ'] = '100.00';
}
if ( $arrLoadTime['SQ_ExcludedVar'] > $arrLoadTime['DQ_ExcludedVar'] )
{
$arrPercent['SQ_ExcludedVar'] = getFullTimePercentRounded(
$arrLoadTime['DQ_ExcludedVar'],
$arrLoadTime['SQ_ExcludedVar']
);
$arrPercent['DQ_ExcludedVar'] = '100.00';
$arrPercent['DQ_IncludedVar'] = getFullTimePercentRounded(
$arrLoadTime['DQ_ExcludedVar'],
$arrLoadTime['DQ_IncludedVar']
);
}
elseif ( $arrLoadTime['SQ_ExcludedVar'] < $arrLoadTime['DQ_ExcludedVar'] )
{
$arrPercent['SQ_ExcludedVar'] = '100.00';
$arrPercent['DQ_ExcludedVar'] = getFullTimePercentRounded(
$arrLoadTime['SQ_ExcludedVar'],
$arrLoadTime['DQ_ExcludedVar']
);
$arrPercent['DQ_IncludedVar'] = getFullTimePercentRounded(
$arrLoadTime['SQ_ExcludedVar'],
$arrLoadTime['DQ_IncludedVar']
);
}
else
{
$arrPercent['SQ_ExcludedVar'] = '100.00';
$arrPercent['DQ_ExcludedVar'] = '100.00';
$arrPercent['DQ_IncludedVar'] = getFullTimePercentRounded(
$arrLoadTime['SQ_ExcludedVar'],
$arrLoadTime['DQ_IncludedVar']
);
}
$nl = "\n";
echo '<pre>';
echo $intRounds . ' rounds with ' . $intAllocations . ' allocations:' . $nl;
echo '+---------------------------------+-' . str_pad( '', $intLength , '-', STR_PAD_RIGHT ) . '---------+----------+' . $nl;
echo '| allocation type | ' . str_pad( 'avg time per round', $intLength + 8, ' ', STR_PAD_RIGHT ) . ' | percent |' . $nl;
echo '+---------------------------------+-' . str_pad( '', $intLength , '-', STR_PAD_RIGHT ) . '---------+----------+' . $nl;
echo '| Single Quotes normal | ' . str_pad( $arrLoadTime['SQ'], $intLength , ' ', STR_PAD_RIGHT ) . ' seconds | ' . str_pad( $arrPercent['SQ'], 5, ' ', STR_PAD_LEFT ) . ' % |' . $nl;
echo '| Double Quotes normal | ' . str_pad( $arrLoadTime['DQ'], $intLength , ' ', STR_PAD_RIGHT ) . ' seconds | ' . str_pad( $arrPercent['DQ'], 5, ' ', STR_PAD_LEFT ) . ' % |' . $nl;
echo '| | ' . str_pad( '', $intLength , ' ', STR_PAD_RIGHT ) . ' | |' . $nl;
echo '| Single Quotes with excluded Var | ' . str_pad( $arrLoadTime['SQ_ExcludedVar'], $intLength , ' ', STR_PAD_RIGHT ) . ' seconds | ' . str_pad( $arrPercent['SQ_ExcludedVar'], 5, ' ', STR_PAD_LEFT ) . ' % |' . $nl;
echo '| Double Quotes with excluded Var | ' . str_pad( $arrLoadTime['DQ_ExcludedVar'], $intLength , ' ', STR_PAD_RIGHT ) . ' seconds | ' . str_pad( $arrPercent['DQ_ExcludedVar'], 5, ' ', STR_PAD_LEFT ) . ' % |' . $nl;
echo '| Double Quotes with included Var | ' . str_pad( $arrLoadTime['DQ_IncludedVar'], $intLength , ' ', STR_PAD_RIGHT ) . ' seconds | ' . str_pad( $arrPercent['DQ_IncludedVar'], 5, ' ', STR_PAD_LEFT ) . ' % |' . $nl;
echo '+---------------------------------+-' . str_pad( '', $intLength , '-', STR_PAD_RIGHT ) . '---------+----------+' . $nl;
echo '</pre>';
?>
Das Ergebnis auf meinem Krepelserver mit PHP 5.3.8:
Code:
100 rounds with 100000 allocations:
+---------------------------------+----------------------+----------+
| allocation type | avg time per round | percent |
+---------------------------------+----------------------+----------+
| Single Quotes normal | 0.0173615452 seconds | 100.00 % |
| Double Quotes normal | 0.0184129881 seconds | 106.06 % |
| | | |
| Single Quotes with excluded Var | 0.0315569425 seconds | 100.00 % |
| Double Quotes with excluded Var | 0.0316055964 seconds | 100.15 % |
| Double Quotes with included Var | 0.0388333392 seconds | 123.06 % |
+---------------------------------+----------------------+----------+
Wenn ihr den Benchmark bei euch ausführt, dann wäre es nett, wenn ihr das Ergebnis auch posten könntet.
Gruß