Skip to content

Instantly share code, notes, and snippets.

@cboden
Last active August 29, 2015 14:21
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 cboden/e18ee4b3af40dbc3daa7 to your computer and use it in GitHub Desktop.
Save cboden/e18ee4b3af40dbc3daa7 to your computer and use it in GitHub Desktop.
Exceptions...
<?php
class ExceptionFactory implements ExceptionFactoryInterface {
public function __invoke($msg = '') {
return new \Exception($msg);
}
}
<?php
interface ExceptionFactoryInterface {
/**
* @return \Exception
*/
public function __invoke();
}
Throw new Exception: 2.838799
Throw $exception: 0.540748
<?php
$theThing = function(ExceptionFactoryInterface $factory) {
throw $factory();
};
$sef = new ExceptionFactory;
$nef = new StacklessExceptionFactory;
echo "Throw new Exception: ";
$s = microtime(true);
for ($i = 0; $i < $t; $i++) {
try {
$theThing($sef);
} catch (Exception $e) {
}
}
printf("%0.6f\n", (microtime(true) - $s));
echo 'Throw $exception: ';
$s = microtime(true);
for ($i = 0; $i < $t; $i++) {
try {
$theThing($nef);
} catch (Exception $e) {
}
}
printf("%0.6f\n", (microtime(true) - $s));
<?php
class StacklessExceptionFactory implements ExceptionFactoryInterface {
private $held;
public function __construct() {
$this->held = new \Exception;
}
public function __invoke() {
return $this->held;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment