Java Try / Catch Block的基准测试 [英] Benchmark of Java Try/Catch Block

查看:120
本文介绍了Java Try / Catch Block的基准测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在执行程序时进入catch块有一些显着的成本,但是,我想知道是否进入try {}块也有任何影响因此我开始在google中寻找一个有很多意见的答案,但是根本没有基准。我发现的一些答案是:

I know that going into a catch block has some significance cost when executing a program, however, I was wondering if entering a try{} block also had any impact so I started looking for an answer in google with many opinions, but no benchmarking at all. Some answers I found were:


  1. Java try / catch性能,是否建议将try子句中的内容保持在最低限度?

  2. 尝试抓取性能Java

  3. Java try catch blocks

  1. Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum?
  2. Try Catch Performance Java
  3. Java try catch blocks

然而他们没有用事实回答我的问题,所以我决定亲自尝试。

However they didn't answer my question with facts, so I decided to try it for myself.

这里是我做了什么。我有一个这种格式的csv文件:

Here's what I did. I have a csv file with this format:

host; ip; number; date; status; email; uid; name; lastname; promo_code;

其中状态之后的所有内容都是可选的,甚至没有相应的内容; ,所以在解析验证时必须要查看值是否存在,这就是我想到的try / catch问题。

where everything after status is optional and will not even have the corresponding ; , so when parsing a validation has to be done to see if the value is there, here's where the try/catch issue came to my mind.

我当前的代码在我公司继承的这样做:

The current code that I inherited in my company does this:

StringTokenizer st=new StringTokenizer(line,";");  
String host = st.nextToken();
String ip = st.nextToken();
String number = st.nextToken();
String date = st.nextToken();
String status = st.nextToken();                             
String email = "";
try{
    email = st.nextToken();
}catch(NoSuchElementException e){
    email = "";
}

并重复使用uid,name,lastname和promo_code为电子邮件做的事情。

and it repeats what it's done for email with uid, name, lastname and promo_code.

我将所有内容更改为:

if(st.hasMoreTokens()){
    email = st.nextToken();
}

实际上它的执行速度更快。解析没有可选列的文件时。以下是平均时间:

and in fact it performs faster. When parsing a file that doesn't have the optional columns. Here are the average times:

 --- Trying:122 milliseconds
 --- Checking:33 milliseconds

然而,这里让我困惑的原因和我要问的原因是:当运行带有可选值的示例时CSV的所有8000行中的列,if()版本仍然比try / catch版本表现更好,所以我的问题是

however, here's what confused me and the reason I'm asking: When running the example with values for the optional columns in all 8000 lines of the CSV, the if() version still performs better than the try/catch version, so my question is

真的尝试block对我的代码没有任何性能影响?

此示例的平均时间为:

--- Trying:105 milliseconds
--- Checking:43 milliseconds

有人能解释一下这里发生了什么吗?

Can somebody explain what's going on here?

非常感谢

推荐答案

是的,try(在Java中)没有任何性能影响。编译器不为try块生成VM语句。它只记录try块处于活动状态的程序计数器,并将此信息附加到类文件中的方法。然后,当抛出异常时,VM展开堆栈并在每个帧检查该帧中的程序计数器是否在相关的try块中。这(与构建堆栈跟踪一起)非常昂贵,因此捕获成本很高。但是,尝试是免费的:)。

Yes, try (in Java) does not have any performance impact. The compiler generates no VM statements for a try block. It simply records the program counters between which the try block is active and attaches this information to the method in the class file. Then, when an exception is thrown, the VM unwinds the stack and checks at each frame whether the program counter in that frame is in a relevant try block. This (together with building the stack trace) is quite costly, so catching is expensive. However, trying is free :).

但是,对常规控制流使用例外不是一个好习惯。

Still, it is not good practice to use exceptions for regular control flow.

您的代码执行速度更快的原因可能是捕获成本非常高,以至于通过简单的尝试替换支票可以节省时间。

The reason why your code performs faster is probably that catching is so extremely costly that it outweights the time saved by replacing the check by a simple try.

尝试捕获在不经常触发catch的代码中可以更快,例如,如果你进入try 10000次但只捕获一次,try方法将比if-check更快。尽管如此,这并不是一种好的风格,你可以选择明确检查更多代币的方式。

Try catch can be faster in code where the catch is triggered not very often, e.g., if you go into the try 10000 times but only catch once, the try method would be faster than the if-check. Still, this is no good style and your way of explicitly checking for more tokens is to be preferred.

这篇关于Java Try / Catch Block的基准测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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