Created
September 20, 2011 09:25
-
-
Save dansimau/1228729 to your computer and use it in GitHub Desktop.
Test scripts for comparing the performance of declaring and instantiating classes dynamically in PHP using eval versus including from files.
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
#!/bin/bash | |
mkdir $1 || exit | |
c=0; | |
while [ $c -lt $1 ]; do | |
cat <<EOF >"$1/Foo_$c.php" | |
<?php | |
Class Foo_$c | |
{ | |
function __construct() { | |
return true; | |
} | |
} | |
?> | |
EOF | |
c=$(($c + 1)) | |
done |
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 | |
// Set memory limit to unlimited | |
ini_set('memory_limit', '-1'); | |
$t = $argv[1]; | |
$start = microtime(true); | |
// Run the loop | |
for ($i = 0; $i < $t; $i++) { | |
// Generate class code for test. Note that there is a significant difference between having | |
// the construct with the return statement than without. We're testing with a return to | |
// simulate "some kind of processing" happening in the class instantiation. | |
$code = <<<EOF | |
Class Foo_$i | |
{ | |
function __construct() { | |
return true; | |
} | |
} | |
EOF; | |
// This eval instantiates the class | |
eval($code); | |
// This eval adds some processing time to assign an instantiated class to a variable | |
$foo = eval("return new Foo_$i;"); | |
} | |
$end = microtime(true); | |
echo "Iterations: " . $t . "\n"; | |
echo "Time: " . round($end - $start, 3) . "s\n"; | |
echo "Memory (real): " . round(memory_get_usage(true) / 1024 / 1024, 2) . "MB\n"; | |
?> |
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 | |
// Set memory limit to unlimited | |
ini_set('memory_limit', '-1'); | |
$t = $argv[1]; | |
$start = microtime(true); | |
// Run the loop | |
for ($i = 0; $i < $t; $i++) { | |
// Include the class code | |
require_once "./" . $t . "/Foo_" . $i . ".php"; | |
// This eval adds some processing time to assign an instantiated class to a variable | |
$foo = eval("return new Foo_$i;"); | |
} | |
$end = microtime(true); | |
echo "Iterations: " . $t . "\n"; | |
echo "Time: " . round($end - $start, 3) . "s\n"; | |
echo "Memory (real): " . round(memory_get_usage(true) / 1024 / 1024, 2) . "MB\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample results:
Declaring and instantiating 10,000 classes using eval:
Declaring 10,000 classes using require_once and instantiating them using eval (without APC):
Declaring 10,000 classes using require_once and instantiating them using eval (with APC):