1

我正在做一些HTML剥离器(用java编写)的性能测试,也就是说,我将一个字符串(实际上是html内容)传递给HTML剥离器的一个方法,后者返回纯文本(没有HTML标签和元信息)。

下面是一个具体实现的例子

public void performanceTest() throws IOException {
    long totalTime;
    File file = new File("/directory/to/ten/different/htmlFiles");
    for (int i = 0; i < 200; ++i) {
        for (File fileEntry : file.listFiles()) {

            HtmlStripper stripper = new HtmlStripper();
            URL url = fileEntry.toURI().toURL();
            InputStream inputStream = url.openStream();
            String html = IOUtils.toString(inputStream, "UTF-8");
            long start = System.currentTimeMillis();
            String text = stripper.getText(html);
            long end = System.currentTimeMillis();
            totalTime = totalTime + (end - start);

      //The duration for the stripping of each file is computed here
     // (200 times for each time). That duration value decreases and then becomes constant
     //IMHO if the duration for the same file should always remain the same.
     //Or is a cache technique used by the JVM?         


        System.out.println("time needed for stripping current file: "+ (end -start));
        }
    }
    System.out.println("Average time for one document: "
            + (totalTime / 2000));

}

但是每个文件的剥离持续时间每次计算200次,并且具有不同的递减值。恕我直言,如果同一个文件 X 的持续时间应该始终保持不变!?或者是JVM使用的缓存技术?

任何帮助,将不胜感激。提前致谢

贺拉斯

注意: - 我正在我的机器上进行本地测试(没有远程,没有 http)。- 我在 Ubuntu 10.04 上使用 java 6

4

2 回答 2

4

这是完全正常的。JIT 将方法编译为本机代码,并随着它们的使用越来越多,对其进行更多的优化。(您的基准最终收敛到的“常数”是 JIT 优化能力的峰值。)

开始计时之前,如果不多次运行该方法,就无法在 Java 中获得良好的基准。

于 2012-05-03T16:06:50.223 回答
0

恕我直言,如果同一个文件 X 的持续时间应始终保持不变

不存在优化的即时编译器。除其他外,它会跟踪使用特定方法/分支的次数,并有选择地将 Java 字节码编译为本机代码。

于 2012-05-03T16:08:03.050 回答