关于debug使用及debug性能报告

来源:互联网 发布:淘宝买的东西不合适 编辑:程序博客网 时间:2024/06/06 01:22

        日本客户说debug多了会影响性能,即使开关没有开,但要判断很多次的话性能肯定要低,因此这边还是作了实验。

调用下面这个类中的method( )方法。



分别是调用10000次、100000次、1000000次,10000000次的时间上的差异:

调用10000次:

使用LOG4J,并在每个方法开始和结尾都输出DEBUG信息,但是LOG级别设置为INFO的场合: 用时:266mm

不使用LOG4J的场合:用时109mm

两种方式的时间相差:157mm

 

调用100000次:

使用LOG4J,并在每个方法开始和结尾都输出DEBUG信息,但是LOG级别设置为INFO的场合: 用时:1250mm

不使用LOG4J的场合:用时1078mm

两种方式的时间相差:172mm

 

调用1000000次:

使用LOG4J,并在每个方法开始和结尾都输出DEBUG信息,但是LOG级别设置为INFO的场合: 用时:11500mm

不使用LOG4J的场合:用时11031mm

两种方式的时间相差:469mm

 

调用10000000次:

使用LOG4J,并在每个方法开始和结尾都输出DEBUG信息,但是LOG级别设置为INFO的场合: 用时:127969mm

不使用LOG4J的场合:用时122766mm

两种方式的时间相差:5203mm

 

===============================================================================

     但是在我的机子上,当调用10000000次这个差值都是负的。。就是说完全不用debug的比写了debug的要慢。这样肯定是有问题的。。。。

后来将程序中的Long全部改为int型,得出:

===============================================================================


调用10000次:

使用LOG4J,并在每个方法开始和结尾都输出DEBUG信息,但是LOG级别设置为WARN的场合: 用时:110mm

不使用LOG4J的场合:用时15mm

两种方式的时间相差:95mm


调用100000次:

使用LOG4J,并在每个方法开始和结尾都输出DEBUG信息,但是LOG级别设置为WARN的场合: 用时:234mm

不使用LOG4J的场合:用时125mm

两种方式的时间相差:109mm


调用1000000次:

使用LOG4J,并在每个方法开始和结尾都输出DEBUG信息,但是LOG级别设置为WARN的场合: 用时:1407mm

不使用LOG4J的场合:用时1109mm

两种方式的时间相差:298mm

 

 

调用10000000次:

使用LOG4J,并在每个方法开始和结尾都输出DEBUG信息,但是LOG级别设置为WARN的场合: 用时:12972mm

不使用LOG4J的场合:用时10956mm

两种方式的时间相差:2016mm


================================================================

可以看出用基本型还是包装类差别还是很大的

一般来说系统的性能不取决于DEBUG的性能,而是开发人员编程上的很多小细节上。


另外,为什么要使用下面的方式:

if(logger.isDebugEnabled()){
      logger.debug("abc" + “cdb” + "efg");
}


如果打印简单的字符串,直接使用debug一般没什么意见:

logger.debug("abc");


但是如果debug的内容使用字符串连接或者运算,那应该使用logger.isDebugEnabled()先判断一下。这个你懂的。

================================================================



 

 ================================================================

附件测试代码

=================================================================

没有debug的:

 

public class NoDebugTest

{  

    public static void method( )

    {

        count1();

        count2();

        count3();

        count4();

        count5();

    }

 

    private static Long count1( )

    {

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        return result;

    }

 

    private static Long count2( )

    {

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        return result;

    }

 

    private static Long count3( )

    {

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        return result;

    }

 

    private static Long count4( )

    {

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        return result;

    }

 

    private static Long count5( )

    {

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        return result;

    }

 

}

 

有debug的:

import org.apache.log4j.Logger;

 

public class HaveDebugTest

{

    private final static Logger log = Logger.getLogger(HaveDebugTest.class);

 

    public static void method( )

    {

        log.debug("enter method");

 

        count1();

        count2();

        count3();

        count4();

        count5();

 

        log.debug("exit method");

    }

 

    private static Long count1( )

    {

        log.debug("enter count");

 

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        log.debug("exit count");

 

        return result;

    }

 

    private static Long count2( )

    {

        log.debug("enter count");

 

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        log.debug("exit count");

 

        return result;

    }

 

    private static Long count3( )

    {

        log.debug("enter count");

 

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        log.debug("exit count");

 

        return result;

    }

 

    private static Long count4( )

    {

        log.debug("enter count");

 

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        log.debug("exit count");

 

        return result;

    }

 

    private static Long count5( )

    {

        log.debug("enter count");

 

        Long result = 0L;

 

        for (int i = 0; i < 100; i++)

        {

            result += i;

        }

 

        log.debug("exit count");

 

        return result;

    }

 

}

0 0
原创粉丝点击