Java if vs. try / catch开销 [英] Java if vs. try/catch overhead

查看:189
本文介绍了Java if vs. try / catch开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中是否存在使用 try / catch 块的开销,而不是如果块(假设附带的代码否则不要求)?



例如,对字符串执行以下两个简单的安全修剪方法实现:

  public String tryTrim(String raw){
try {
return raw.trim();
} catch(Exception e){
}
return null;
}

public String ifTrim(String raw){
if(raw == null){
return null;
}
return raw.trim();
}

如果 raw 输入只是很少 null 两种方法之间是否存在任何性能差异



此外,使用 tryTrim()方法来简化代码的布局,这是一个很好的编程模式,特别是当许多 if块检查罕见的错误条件可以通过将代码包含在一个try / catch块中来避免?



例如,有一个方法是常见的情况使用 N个参数,它们在其开始附近使用 M <= N ,如果有任何这样的参数为无效(例如,空或空字符串),而不影响其余代码。



在这种情况下,不必写 k * M 如果块(其中 k 是每个参数的平均检查次数,例如 k = 2 为null或emp ty字符串),一个 try / catch 块将大大缩短代码,并且可以使用1-2行注释来明确地说明非常规逻辑。



这样的模式也将加快方法,特别是如果错误条件很少出现,并且在不影响程序安全性的情况下(假定错误条件为正常,例如如在一个字符串处理方法中,空值或空值可以接受,尽管很少存在)。

解决方案

我知道你是询问性能开销,但是您真的不应该使用 try / catch code>可互换。



尝试 / catch 是出于错误的事情,不在你的控制之下,而不是正常的程序流程。例如,尝试写入文件并且文件系统已满?这种情况通常应用尝试 / catch



如果语句应该是正常流程和普通错误检查。那么,例如,用户无法填充所需的输入字段?使用,如果,而不是尝试 / catch 。 / p>

在我看来,您的示例代码强烈表明,正确的方法是 if 语句而不是尝试 / catch



为了回答你的问题,我假设在尝试 / catch 中的开销大于$ code> code>。要确定,请获取一个Java分析器,并找出您关心的特定代码。有可能答案可能因情况而异。


Is there any overhead in Java for using a try/catch block, as opposed to an if block (assuming that the enclosed code otherwise does not request so)?

For example, take the following two simple implementations of a "safe trim" method for strings:

public String tryTrim(String raw) {
    try {
        return raw.trim();
    } catch (Exception e) {
    }
    return null;
}

public String ifTrim(String raw) {
    if (raw == null) {
        return null;
    }
    return raw.trim();
}

If the raw input is only rarely null, is there any performance difference between the two methods?

Furthermore, is it a good programming pattern to use the tryTrim() approach for simplifying the layout of code, especially when many if blocks checking rare error conditions can be avoided by enclosing the code in one try/catch block?

For example, it is a common case to have a method with N parameters, which uses M <= N of them near its start, failing quickly and deterministically if any such parameter is "invalid" (e.g., a null or empty string), without affecting the rest of the code.

In such cases, instead of having to write k * M if blocks (where k is the average number of checks per parameter, e.g. k = 2 for null or empty strings), a try/catch block would significantly shorten the code and a 1-2 line comment could be used to explicitly note the "unconventional" logic.

Such a pattern would also speed up the method, especially if the error conditions occur rarely, and it would do so without compromising program safety (assuming that the error conditions are "normal", e.g. as in a string processing method where null or empty values are acceptable, albeit seldom in presence).

解决方案

I know you're asking about performance overhead, but you really should not use try/catch and if interchangeably.

try/catch is for things that go wrong that are outside of your control and not in the normal program flow. For example, trying to write to a file and the file system is full? That situation should typically be handled with try/catch.

if statements should be normal flow and ordinary error checking. So, for example, user fails to populate a required input field? Use if for that, not try/catch.

It seems to me that your example code strongly suggests that the correct approach there is an if statement and not a try/catch.

To answer your question, I would surmise that there is generally more overhead in a try/catch than an if. To know for sure, get a Java profiler and find out for the specific code you care about. It's possible that the answer may vary depending on the situation.

这篇关于Java if vs. try / catch开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆