Stringquotes Benchmark

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!^^):
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ß :)
 
Um die Frage zu beantworten:
Doppelte quotes sind langsamer, weil der Inhalt noch extra geparset werden muss.
 
Dem würde ich nicht unbedingt zustimmen.

Ab PHP 5.3 scheint sich das nicht mehr viel zu nehmen, bei mir sind teilweise sogar die Doublequotes schneller.
 
Code:
+---------------------------------+----------------------+----------+
| allocation type                 | avg time per round   | percent  |
+---------------------------------+----------------------+----------+
| Single Quotes normal            | 0.0158899305 seconds | 101.83 % |
| Double Quotes normal            | 0.0156048441 seconds | 100.00 % |
|                                 |                      |          |
| Single Quotes with excluded Var | 0.0246998026 seconds | 100.00 % |
| Double Quotes with excluded Var | 0.0248399165 seconds | 100.57 % |
| Double Quotes with included Var | 0.0281087138 seconds | 113.8 % |
+---------------------------------+----------------------+----------+

Code:
+---------------------------------+----------------------+----------+
| allocation type                 | avg time per round   | percent  |
+---------------------------------+----------------------+----------+
| Single Quotes normal            | 0.0159713788 seconds | 100.00 % |
| Double Quotes normal            | 0.0160026549 seconds | 100.2 % |
|                                 |                      |          |
| Single Quotes with excluded Var | 0.0256873367 seconds | 100.00 % |
| Double Quotes with excluded Var | 0.0257996272 seconds | 100.44 % |
| Double Quotes with included Var | 0.0294439961 seconds | 114.62 % |
+---------------------------------+----------------------+----------+

Zwei Tests - selber Server, selber FTP, alles gleich - kurz hintereinander, damit die Objektivität gegeben ist.

Nachtrag: Ich hab PHP 5.3.3 laufen...
 
Zuletzt bearbeitet:
Dem kann ich nicht zustimmen. Zumindest mit beinhalteten Variablen brauchen double Quotes eindeutig länger.
 
Wie man bei den Ergebnissen auch gut sehen kann. Aber solange man Variablen raus lässt, spielt es kaum eine Rolle.
 
Ich unterscheide aber auch wegen der Lesbarkeit, nicht nur wegen der Performance
Beginnt bei mir ein String mit einem ', dann weiss ich gleich, dass da keine Variable drin ist. Also muss ich da höchstens auf auf Rechtschreibung prüfen
Beginnt er aber mit ", dann habe da etwas das geparst wird.
 
Zurück