首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在php中try-catch的性能

在php中try-catch的性能
EN

Stack Overflow用户
提问于 2008-09-19 18:29:01
回答 7查看 30.8K关注 0票数 65

在php 5中使用try-catch语句时,需要考虑哪些性能问题?

我以前在网上读过一些关于这个主题的古老的和似乎相互矛盾的信息。我目前使用的很多框架都是在php 4上创建的,缺乏php 5的许多细节,所以我自己在php中使用try-catchs的经验并不多。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2008-09-19 18:58:02

需要考虑的一件事是,不抛出异常的try块的成本与实际抛出和捕获异常的成本是不同的问题。

如果只有在失败的情况下才抛出异常,那么几乎可以肯定的是,您并不关心性能,因为每次执行程序时都不会失败很多次。如果你在一个严密的循环中失败(也就是说:用头撞砖墙),那么你的应用程序的问题可能比速度慢更严重。因此,不要担心抛出异常的成本,除非您以某种方式被迫将其用于常规控制流。

有人发布了一个关于分析抛出异常的代码的答案。我自己从来没有测试过它,但我自信地预测,这将比仅仅进出try块而不抛出任何东西带来更大的性能影响。

另一件要考虑的事情是,当你在很多层次上嵌套调用时,在顶部设置一个try...catch甚至比检查返回值并在每次调用时传播错误更快。

在相反的情况下,您会发现将每个调用都包装在它自己的try...catch块中,您的代码将会变慢。也更丑陋。

票数 72
EN

Stack Overflow用户

发布于 2009-01-14 23:28:00

我很无聊,并分析了以下内容(我省略了计时代码):

代码语言:javascript
复制
function no_except($a, $b) { 
    $a += $b;
    return $a;
}
function except($a, $b) { 
    try {
        $a += $b;
    } catch (Exception $e) {}
    return $a;
}

使用两个不同的循环:

代码语言:javascript
复制
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次,运行ApacheandPHP5.2.6:

代码语言:javascript
复制
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

这些结果是一致的,并且无论测试运行的顺序如何,这些结果都保持在类似的比例中。

结论:添加代码来处理罕见的异常并不比忽略异常的代码慢。

票数 57
EN

Stack Overflow用户

发布于 2013-07-17 03:07:25

Try-catch块不是性能问题-真正的性能瓶颈来自于创建异常对象。

测试代码:

代码语言:javascript
复制
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";
}

结果:

代码语言:javascript
复制
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
票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/104329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档