2015-06-13 24 views
3

我試圖使用在這裏提到的java的風向標: https://www.ibm.com/developerworks/java/library/j-benchmark2/ 並且可以在這裏下載: http://www.ellipticgroup.com/html/benchmarkingArticle.html的Java基準測試中Ellipticgroup /布倫特·博耶

我試圖用簡單的例子在文章上方:

import bb.util.Benchmark; 

public class ShortIndexesLoop { 

    public static void main(String[] args) throws Exception { 
     Callable<Integer> task = 
      new Callable<Integer>() { public Integer call() { return fibonacci(35); } }; 
      System.out.println("fibonacci(35): " + new Benchmark(task)); 
    } 

    protected static int fibonacci(int n) throws IllegalArgumentException { 
     if (n < 0) throw new IllegalArgumentException("n = " + n + " < 0"); 
     if (n <= 1) return n; 
     return fibonacci(n - 1) + fibonacci(n - 2); 
    } 
} 

但是這個基準的每一個執行它在運行基準測試之前,一個異常開始

6月13日,2015 1:45:37 PM StringUtil 警告:字符串行爲不像預期的那樣;看到引起

java.lang.Exception: substring does NOT share the same underlying char[] with its parent String 
    at bb.util.StringUtil.inspectStringConstructor(StringUtil.java:84) 
    at bb.util.StringUtil.<clinit>(StringUtil.java:75) 
    at bb.io.ConsoleUtil.<clinit>(ConsoleUtil.java:81) 
    at bb.util.Benchmark.sendUserMsg(Benchmark.java:1002) 
    at bb.util.Benchmark.osSpecificPreparation(Benchmark.java:579) 
    at bb.util.Benchmark.perform(Benchmark.java:541) 
    at bb.util.Benchmark.<init>(Benchmark.java:464) 
    at bb.util.Benchmark.<init>(Benchmark.java:439) 
    at ShortIndexesLoop.main(fib.java:13) 

似乎到基準任何呼叫結束使用以下方法StringUtil

private static final boolean stringContructorTrimsGarbage = inspectStringConstructor(); 

private static boolean inspectStringConstructor() { 
    try { 
      // first prove that substring shares the same underlying char[] as the parent String: 
     String s1 = "abc123def"; 
     String s2 = s1.substring(3, 6); 
     char[] value1 = (char[]) ReflectUtil.get(s1, "value"); 
     char[] value2 = (char[]) ReflectUtil.get(s2, "value"); 
     if (value1 != value2) throw new Exception("substring does NOT share the same underlying char[] with its parent String"); 
     if (value2.length != s1.length()) throw new Exception("value2.length = " + value2.length + " != s1.length() = " + s1.length()); 

      // second prove that the String(String) constructor trims garbage chars: 
     String s3 = new String(s2); 
     char[] value3 = (char[]) ReflectUtil.get(s3, "value"); 
     if (value3 == value2) throw new Exception("new String shares the same underlying char[] with its String arg"); 
     if (!(value3.length < value2.length)) throw new Exception("value3.length = " + value3.length + " is not < value2.length = " + value2.length); 

     return true; 
    } 
    catch (Exception e) { 
     LogUtil.getLogger2().logp(Level.WARNING, "StringUtil", "<clinit>", "String does not behave as expected; see cause", e); 
     return false; 
    } 
} 

我不介意例外,因爲基準庫做給一個結果。但是,試圖將此代碼編譯爲jar並將其作爲jar運行,這是一個問題。 由於例外其餘代碼將不會在終端執行

任何人都有任何想法如何解決這個問題?

+0

可能與Java 7中的實現更改不兼容:http://java.dzone.com/articles/changes-stringsubstring-java-7 – Marvin

+0

有沒有辦法在不更改爲java 1.6或1.8的情況下處理此問題? – Quantico

+1

Java 8不會幫助,因爲此更改仍然存在。除此之外,您只能等待庫更新或嘗試自行修補它。編輯:或者嘗試一個不同的c庫,但我無法推薦,甚至不能提名。 – Marvin

回答

2

我知道這是一個老問題,但是我的解決方案對其他人也是有用的:在這個基準測試框架的StringUtil類別的源文件中,記錄了爲什麼要進行這種檢查。基於此,如果您有一個JDK,其中substring創建了基礎字符數組相關部分的副本,則可以簡單地評論stringContructorTrimsGarbage標誌,並在newString方法中簡單地返回s。