Skip to content

Instantly share code, notes, and snippets.

@Furgas
Last active September 17, 2018 07:09
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 Furgas/d19ca419969337a341b58a3d6eb7b9ce to your computer and use it in GitHub Desktop.
Save Furgas/d19ca419969337a341b58a3d6eb7b9ce to your computer and use it in GitHub Desktop.
Try...catch benchmark
php --version
PHP 7.2.9-1+0~20180910100512.5+stretch~1.gbpdaac35 (cli) (built: Sep 10 2018 10:05:13) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.9-1+0~20180910100512.5+stretch~1.gbpdaac35, Copyright (c) 1999-2018, by Zend Technologies
php test.php
no except with no surrounding try: 2,2994790077209
no except with surrounding try: 2,427591085434
5,277333504877% slower
except with no surrounding try: 2,5101630687714
except with surrounding try: 2,5217618942261
0,45994927123251% slower
php test.php
no except with no surrounding try: 2,2707672119141
no except with surrounding try: 2,3757629394531
4,4194530437128% slower
except with no surrounding try: 2,4595458507538
except with surrounding try: 2,4893519878387
1,1973452219924% slower
php test.php
no except with no surrounding try: 2,280993938446
no except with surrounding try: 2,3965029716492
4,8198994355361% slower
except with no surrounding try: 2,4775400161743
except with surrounding try: 2,5066699981689
1,1620988010351% slower
php test.php
no except with no surrounding try: 2,2379238605499
no except with surrounding try: 2,3840081691742
6,1276765119001% slower
except with no surrounding try: 2,4718890190125
except with surrounding try: 2,5106120109558
1,5423726077299% slower
php test.php
no except with no surrounding try: 2,2698709964752
no except with surrounding try: 2,3785450458527
4,5689296306131% slower
except with no surrounding try: 2,4950959682465
except with surrounding try: 2,512405872345
0,68897721857155% slower
<?php
function no_except($a, $b) {
$a += $b;
return $a;
}
function except($a, $b) {
try {
$a += $b;
} catch (Exception $e) {}
return $a;
}
define('NUM_TESTS', 100000000);
echo 'no except with no surrounding try: ';
$start = microtime(true);
for ($i = 0; $i < NUM_TESTS; ++$i) {
no_except(5, 7);
}
$end = microtime(true);
echo ($without_try = $end - $start) . "\n";
echo 'no except with surrounding try: ';
$start = microtime(true);
for ($i = 0; $i < NUM_TESTS; ++$i) {
try {
no_except(5, 7);
} catch (Exception $e) {}
}
$end = microtime(true);
echo ($with_try = $end - $start) . "\n";
echo ($difference = ($with_try - $without_try) / $with_try * 100) . '% ' . ($difference > 0 ? 'slower' : 'faster') . "\n";
echo 'except with no surrounding try: ';
$start = microtime(true);
for ($i = 0; $i < NUM_TESTS; ++$i) {
except(5, 7);
}
$end = microtime(true);
echo ($without_try = $end - $start) . "\n";
echo 'except with surrounding try: ';
$start = microtime(true);
for ($i = 0; $i < NUM_TESTS; ++$i) {
try {
except(5, 7);
} catch (Exception $e) {}
}
$end = microtime(true);
echo ($with_try = $end - $start) . "\n";
echo ($difference = ($with_try - $without_try) / $with_try * 100) . '% ' . ($difference > 0 ? 'slower' : 'faster') . "\n";
@Furgas
Copy link
Author

Furgas commented Sep 17, 2018

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