日本客戶說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;
}
}