Ellipticgroup/Brent Boyer 的 Java 基准测试

标签 java benchmarking

我正在尝试使用此处提到的 Java 基准测试: https://www.ibm.com/developerworks/java/library/j-benchmark2/ 可以在这里下载: http://www.ellipticgroup.com/html/benchmarkingArticle.html

我尝试使用上面文章中的基本示例:

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);
    }
}

但是这个基准测试的每次执行都在运行基准测试之前以异常开始

2015 年 6 月 13 日下午 1:45:37 StringUtil 警告:String 的行为不符合预期;查看原因

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 运行是一个问题。 由于异常,其余代码将不会在终端中执行

有人知道如何解决这个问题吗?

最佳答案

我知道这是一个老问题,但也许我的解决方案对其他人也有用:在这个基准框架的 StringUtil 类的源代码中,记录了为什么要进行此检查.基于此,如果您有一个 JDK,其中 substring 创建底层 char 数组相关部分的副本,您可以简单地注释 stringContructorTrimsGarbage 标志,并在 newString 方法,你只需返回 s。

关于Ellipticgroup/Brent Boyer 的 Java 基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30821746/

相关文章:

java - 是否可以使用 java 接口(interface)或 bean 启动 Camel 路线?

java - 更新 SQlite 数据库中的图像

java - 用序号字符在java中打印日期

performance - Web 应用程序性能基准的建议

java - JMH 中的操作数到底是多少?

c - Nasa paralell 基准测试 - MPI 错误

linux - Linux调度器的性能评估

java: if 语句跳过后面的 if 语句

java - 我如何知道点击了哪个提交按钮

performance - Symfony2 多内核?