Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created May 25, 2015 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmikola/d21a54dc25a041505eac to your computer and use it in GitHub Desktop.
Save jmikola/d21a54dc25a041505eac to your computer and use it in GitHub Desktop.
Benchmarking PHP exception throws and backtrace generation
$ php bench.php
Throwing: 0.060640
Assigning and throwing: 0.064791
Assigning once and throwing: 0.011064
<?php
echo "Throwing: ";
$s = microtime(true);
for ($i = 0; $i < 100000; $i++) {
try {
throw new Exception;
} catch (Exception $_) {}
}
printf("%0.6f\n", (microtime(true) - $s));
echo "Assigning and throwing: ";
$s = microtime(true);
for ($i = 0; $i < 100000; $i++) {
try {
$e = new Exception;
throw $e;
} catch (Exception $_) {}
}
printf("%0.6f\n", (microtime(true) - $s));
echo "Assigning once and throwing: ";
$s = microtime(true);
$e = new Exception;
for ($i = 0; $i < 100000; $i++) {
try {
throw $e;
} catch (Exception $_) {}
}
printf("%0.6f\n", (microtime(true) - $s));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment