Do php 5 try catches affect performance when no exception is caught?
Someone extremely smart at work told me try catches which don't throw will affect performance on a site with millions of users. based on the unit test posted showing equal performance, I'm w开发者_运维百科ondering if this is related to an os level and/or web server specific situation. For instance, web server's implementation of asynchronous work occurs on child processes instead of threads.
Anyone know?
What I'd like to see is an output of a php profiler showing actual cpu time used.
jmucchiello's comment on Performance of try-catch in php is interesting, but doesn't measure cpu time used.
Thanks, David
I ran a very small and unscientific test and found there to be roughly no difference between having a catch that never gets called or having it outside a try catch statement. I ran each of these two scripts 5 times each through my profiler and averaged the total times.
Results
- With Try/Catch statement: 1.34 seconds
- Without Try/Catch statement: 1.3 seconds
The Code
Try/Catch
<?php
foreach(range(1,200000) as $i) {
try {
if ($i % 5 == 0 && $i % 3 == 0) echo "fizzbuzz";
elseif ($i % 5 == 0) echo "fizz";
elseif ($i % 3 == 0) echo "buzz";
else echo $i;
} catch (Exception $e) {
echo sin($i) * cos($i * pi());
}
}
?>
No Try/Catch
<?php
foreach(range(1,200000) as $i) {
if ($i % 5 == 0 && $i % 3 == 0) echo "fizzbuzz";
elseif ($i % 5 == 0) echo "fizz";
elseif ($i % 3 == 0) echo "buzz";
else echo $i;
}
?>
精彩评论