在日誌系統中,每個日誌輸出用的方法的輔助類如這個做一個Thread.getStackTrace()有多昂貴?
public void debug(String message) {
Logger logger = Logger.getLogger(getCallingClass());
logger.debug(message);
}
...
public Class getCallingClass() {
/*
Calls Thread.getStackTrace() and back traces until the class on the stack trace
!= this.getClass().
*/
return classFound;
}
這種運行需要多少費用,可能它有顯著的性能命中?
而不是使用布爾標誌這種情況可以使用記錄器。isDebugEnabled() – Inv3r53 2010-02-27 15:37:03
更好的方法是使用預處理指令。 – Orentet 2010-02-27 15:38:58
這對兩者都有好處。使用靜態最終布爾值,編譯器將只刪除if()語句中的任何內容,因此它就像預處理器指令;) – Kylar 2010-02-27 16:04:24