例外情况如何 [英] How expensive are Exceptions

查看:124
本文介绍了例外情况如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你知道java中异常抛出和处理的代价是多少?



我们已经对我们团队中的例外情况进行了几次讨论。有些人尽可能多地避免这种情况,有人说使用异常的性能下降被高估了。



今天我在我们的软件中找到了以下代码:

  private void doSomething()
{
try
{
doSomethingElse();
}
catch(DidNotWorkException e)
{
log(A Message);
}
goOn();
}
private void doSomethingElse()
{
if(isSoAndSo())
{
throw new DidNotWorkException();
}
goOnAgain();
}


相比,性能如何

  private void doSomething()
{
doSomethingElse();
goOn();
}
private void doSomethingElse()
{
if(isSoAndSo())
{
log(A Message);
返回;
}
goOnAgain();
}

我不想讨论代码美学或任何东西,只是关于运行时行为!
你有真实的体验/测量吗?

解决方案

我没有想过阅读例外,非常快速的测试与你的一些修改的代码我得出的结论是异常情况比布尔的情况相当慢很多。



我得到以下结果: p>

 异常:20891ms 
布尔值:62ms

从此代码中:

  public class Test {
public static void main(String args []){
Test t = new Test();
t.testException();
t.testBoolean();
}
public void testException(){
long start = System.currentTimeMillis();
for(long i = 0; i< = 10000000L; ++ i)
doSomethingException();
System.out.println(Exception:+(System.currentTimeMillis() - start)+ms);
}
public void testBoolean(){
long start = System.currentTimeMillis();
for(long i = 0; i< = 10000000L; ++ i)
doSomething();
System.out.println(Boolean:+(System.currentTimeMillis() - start)+ms);
}

private void doSomethingException(){
try {
doSomethingElseException();
} catch(DidNotWorkException e){
// Msg
}
}
private void doSomethingElseException()throws DidNotWorkException {
if(!isSoAndSo() ){
throw new DidNotWorkException();
}
}
private void doSomething(){
if(!doSomethingElse())
; // Msg
}
private boolean doSomethingElse(){
if(!isSoAndSo())
return false;
返回true;
}
private boolean isSoAndSo(){return false; }
public class DidNotWorkException extends Exception {}
}

我愚蠢没有'我的代码很好,以前有一个错误(如何令人尴尬),如果有人可以三倍检查这个代码,我会非常喜欢它,以防万一我要老了。



我的规格是:




  • 编译并运行于1.5.0_16

  • Sun JVM

  • WinXP SP3

  • Intel Centrino Duo T7200(2.00Ghz,977Mhz)

  • 2.00 GB Ram



在我看来,您应该注意到,非异常方法不会在doSomethingElse中给出日志错误,而是返回一个布尔值以便调用代码可以处理故障。如果有多个区域可能会失败,那么可能需要记录错误或抛出异常。


Do you know how expensive exception throwing and handling in java is?

We had several discussions about the real cost of exceptions in our team. Some avoid them as often as possible, some say the loss of performance by using exceptions is overrated.

Today I found the following piece of code in our software:

private void doSomething()
{
    try
    {
      doSomethingElse();
    }
    catch(DidNotWorkException e)
    {
       log("A Message");
    }
    goOn();
}
private void doSomethingElse()
{
   if(isSoAndSo())
   {
      throw new DidNotWorkException();
   }
   goOnAgain();
}

How is the performance of this compared to

private void doSomething()
{
    doSomethingElse();
    goOn();
}
private void doSomethingElse()
{
   if(isSoAndSo())
   {
      log("A Message");
      return;
   }
   goOnAgain();
}

I don't want to discuss code aesthetic or anything, it's just about runtime behaviour! Do you have real experiences/measurements?

解决方案

I haven't bothered to read up on Exceptions but doing a very quick test with some modified code of yours I come to the conclusion that the Exception circumstance quite a lot slower than the boolean case.

I got the following results:

Exception:20891ms
Boolean:62ms

From this code:

public class Test {
    public static void main(String args[]) {
            Test t = new Test();
            t.testException();
            t.testBoolean();
    }
    public void testException() {
            long start = System.currentTimeMillis();
            for(long i = 0; i <= 10000000L; ++i)
                    doSomethingException();
            System.out.println("Exception:" + (System.currentTimeMillis()-start) + "ms");
    }
    public void testBoolean() {
            long start = System.currentTimeMillis();
            for(long i = 0; i <= 10000000L; ++i)
                    doSomething();
            System.out.println("Boolean:" + (System.currentTimeMillis()-start) + "ms");
    }

    private void doSomethingException() {
        try {
          doSomethingElseException();
        } catch(DidNotWorkException e) {
           //Msg
        }
    }
    private void doSomethingElseException() throws DidNotWorkException {
       if(!isSoAndSo()) {
          throw new DidNotWorkException();
       }
    }
    private void doSomething() {
        if(!doSomethingElse())
            ;//Msg
    }
    private boolean doSomethingElse() {
       if(!isSoAndSo())
          return false;
       return true;
    }
    private boolean isSoAndSo() { return false; }
    public class DidNotWorkException extends Exception {}
}

I foolishly didn't read my code well enough and previously had a bug in it (how embarassing), if someone could triple check this code I'd very much appriciate it, just in case I'm going senile.

My specification is:

  • Compiled and run on 1.5.0_16
  • Sun JVM
  • WinXP SP3
  • Intel Centrino Duo T7200 (2.00Ghz, 977Mhz)
  • 2.00 GB Ram

In my opinion you should notice that the non-exception methods don't give the log error in doSomethingElse but instead return a boolean so that the calling code can deal with a failure. If there are multiple areas in which this can fail then logging an error inside or throwing an Exception might be needed.

这篇关于例外情况如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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