Skip to content

Instantly share code, notes, and snippets.

@belichuk
Created April 15, 2015 12:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belichuk/0ac0030357ff81290af6 to your computer and use it in GitHub Desktop.
Save belichuk/0ac0030357ff81290af6 to your computer and use it in GitHub Desktop.
PHP performance: function vs closures vs
<?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;
@belichuk
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment