在php 5中使用try-catch语句时,要考虑什么样的性能影响?
以前,我已经在网络上阅读了一些有关该主题的古老的,看似矛盾的信息。我目前必须使用的许多框架都是在php 4上创建的,并且缺少php 5的许多优点。因此,我在使用try-catchs与php方面没有太多经验。
[某人发布了一个答案,谈论引发异常的性能分析代码。我从来没有亲自测试过它,但是我有信心地预测,这将带来更大的性能损失,而不仅仅是在不抛出任何尝试的情况下进出try块。
另一件事要考虑的是,在嵌套调用的深度很深的地方,在顶部单独进行一次try ... catch甚至比检查返回值和传播每次调用的错误要快得多。
与这种情况相反,在这种情况下,您发现将每个调用都包装在其自己的try ... catch块中,因此代码会变慢。和丑陋的。
function no_except($a, $b) {
$a += $b;
return $a;
}
function except($a, $b) {
try {
$a += $b;
} catch (Exception $e) {}
return $a;
}
使用两个不同的循环:
echo 'no except with no surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
no_except(5, 7);
}
echo 'no except with surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
try {
no_except(5, 7);
} catch (Exception $e) {}
}
echo 'except with no surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
except(5, 7);
}
echo 'except with surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
try {
except(5, 7);
} catch (Exception $e) {}
}
在WinXP上运行1000000,运行apache和PHP 5.2.6:
no except with no surrounding try = 3.3296
no except with surrounding try = 3.4246
except with no surrounding try = 3.2548
except with surrounding try = 3.2913
这些结果是一致的,并且无论测试按什么顺序运行,都保持相似的比例。
结论:添加代码以处理罕见异常并不比编写忽略异常代码慢。
function shuffle_assoc($array) {
$keys = array_keys($array);
shuffle($keys);
return array_merge(array_flip($keys), $array);
}
$c_e = new Exception('n');
function no_try($a, $b) {
$a = new stdclass;
return $a;
}
function no_except($a, $b) {
try {
$a = new Exception('k');
} catch (Exception $e) {
return $a + $b;
}
return $a;
}
function except($a, $b) {
try {
throw new Exception('k');
} catch (Exception $e) {
return $a + $b;
}
return $a;
}
function constant_except($a, $b) {
global $c_e;
try {
throw $c_e;
} catch (Exception $e) {
return $a + $b;
}
return $a;
}
$tests = array(
'no try with no surrounding try'=>function() {
no_try(5, 7);
},
'no try with surrounding try'=>function() {
try {
no_try(5, 7);
} catch (Exception $e) {}
},
'no except with no surrounding try'=>function() {
no_except(5, 7);
},
'no except with surrounding try'=>function() {
try {
no_except(5, 7);
} catch (Exception $e) {}
},
'except with no surrounding try'=>function() {
except(5, 7);
},
'except with surrounding try'=>function() {
try {
except(5, 7);
} catch (Exception $e) {}
},
'constant except with no surrounding try'=>function() {
constant_except(5, 7);
},
'constant except with surrounding try'=>function() {
try {
constant_except(5, 7);
} catch (Exception $e) {}
},
);
$tests = shuffle_assoc($tests);
foreach($tests as $k=>$f) {
echo $k;
$start = microtime(true);
for ($i = 0; $i < 1000000; ++$i) {
$f();
}
echo ' = '.number_format((microtime(true) - $start), 4)."<br>\n";
}
结果:
no try with no surrounding try = 0.5130
no try with surrounding try = 0.5665
no except with no surrounding try = 3.6469
no except with surrounding try = 3.6979
except with no surrounding try = 3.8729
except with surrounding try = 3.8978
constant except with no surrounding try = 0.5741
constant except with surrounding try = 0.6234
尝试获取记录时发生SQL错误-意外失败,记录可能存在或可能不存在,但是您有程序错误-这是发生异常的好地方-错误日志中记录错误,向管理员发送电子邮件给堆栈进行跟踪,并向用户显示礼貌的错误消息,告知用户出现了问题,您正在对此进行操作。
if(isset($var) && is_array($var)){
foreach($var as $k=>$v){
$var[$k] = $v+1;
}
}
比…快
try{
foreach($var as $k=>$v){
$var[$k] = $v+1;
}
}catch(Exception($e)){
}
我也相信(未经测试):
<?php //beginning code try{ //some more code foreach($var as $k=>$v){ $var[$k] = $v+1; } //more code }catch(Exception($e)){ } //output everything ?>
比在代码中包含额外的IF更为昂贵