Last active
February 22, 2016 16:56
-
-
Save HoffmannP/8247874c8d412d9c48d7 to your computer and use it in GitHub Desktop.
Wie viel Zeit kosten Funktionsaufrufe in PHP?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('MAX', 10000); | |
$varianteA = '/tmp/varianteA.php'; | |
$varianteB = '/tmp/varianteB.php'; | |
function funcName($i) { | |
$n = ''; | |
while ($i > 25) { | |
$n = chr(($i % 26) + 97) . $n; | |
$i = $i / 26; | |
} | |
return '_' . chr($i + 97) . $n; | |
} | |
/*** TEIL A ***/ | |
$source = '<?php | |
ini_set("xdebug.max_nesting_level", ' . (MAX + 4) . ');'; | |
$oldname = '$v = 12'; | |
for ($i = 0; $i < MAX; $i++) { | |
$name = funcName($i); | |
$rnd = rand(-10, 10); | |
$source .= ' | |
function '.$name.'($z) { | |
$z += ' . $rnd .'; | |
' . $oldname . '; | |
$z -= ' . $rnd .'; | |
return $z; | |
}'; | |
$oldname = '$z = ' . $name . '($z)'; | |
} | |
$source .= ' | |
$__a = microtime(); | |
echo ' . $name . '(12) . "\n"; | |
$__b = microtime(); | |
$zeitA = $__b - $__a; | |
echo $zeitA . "\n"; | |
'; | |
file_put_contents($varianteA, $source); | |
echo $varianteA . " erzeugt\n"; | |
/*** TEIL B ***/ | |
$sourcePre = '<?php | |
$__a = microtime(); | |
$z = 12;'; | |
$sourcePost = ' | |
echo $z . "\n"; | |
$__b = microtime(); | |
$zeitB = $__b - $__a; | |
echo $zeitB . "\n"; | |
'; | |
$oldname = '$z'; | |
for ($i = 0; $i < MAX; $i++) { | |
$name = '$' . funcName($i); | |
$rnd = rand(-10, 10); | |
$sourcePre = $sourcePre . ' | |
' . $name . ' = ' . $oldname . '; | |
' . $name . ' += ' . $rnd . ';'; | |
$sourcePost = ' | |
' . $name . ' -= ' . $rnd . '; | |
' . $oldname . ' = ' . $name . ';' . $sourcePost; | |
$oldname = $name; | |
} | |
$source = $sourcePre . ' | |
$z = 12; | |
' . $sourcePost; | |
file_put_contents($varianteB, $source); | |
echo $varianteB . " erzeugt\n"; | |
/* Auswertung */ | |
include($varianteA); | |
include($varianteB); | |
echo 'Mehrkosten durch die ' . MAX . ' Funktionsaufrufe: ' . ($zeitA - $zeitB) . "\n"; | |
echo 'Macht pro Aufruf ' . ($zeitA - $zeitB) / MAX . "s\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment