Created
April 15, 2015 12:12
-
-
Save belichuk/0ac0030357ff81290af6 to your computer and use it in GitHub Desktop.
PHP performance: function vs closures vs
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 | |
$iter = 1000000; | |
$start = microtime(true); | |
function func($item) { return $item; }; | |
for ($i = 0; $i < $iter; $i++) | |
{ | |
func($i); | |
} | |
$end = microtime(true) - $start; | |
echo "Defined function: ".PHP_EOL; | |
echo "$end seconds".PHP_EOL; | |
$start = microtime(true); | |
$fn = function($item) { return $item; }; | |
for ($i = 0; $i < $iter; $i++) | |
{ | |
$fn($i); | |
} | |
$end = microtime(true) - $start; | |
echo "Closure function: ".PHP_EOL; | |
echo "$end seconds".PHP_EOL; | |
$start = microtime(true); | |
$fn = create_function( '$item', 'return $item;' ); | |
for ($i = 0; $i < $iter; $i++) | |
{ | |
$fn($i); | |
} | |
$end = microtime(true) - $start; | |
echo "Create function: ".PHP_EOL; | |
echo "$end seconds".PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHP 5.6.6-2 (cli) (built: Feb 24 2015 10:07:30)
Defined function:
0.098623037338257 seconds
Closure function:
0.094881057739258 seconds
Create function:
0.12652206420898 seconds