简体   繁体   English

jvm缓存方法调用?

[英]jvm caching of method calls?

I am doing some performance tests of a HTML stripper (written in java), that is to say, I pass a string (actually html content) to a method of the HTML stripper and the latter returns plain text (without HTML tags and meta information). 我正在对HTML stripper(用java编写)进行一些性能测试,也就是说,我将一个字符串(实际上是html内容)传递给HTML stripper的一个方法,后者返回纯文本(没有HTML标签和元信息) )。

Here is an example of the concrete implementation 以下是具体实现的示例

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

}

But the duration for the stripping of each file is computed 200 times for each time and has a different decreasing value. 但是每个文件的剥离持续时间每次计算200次,并且具有不同的递减值。 IMHO if the duration for one and the same file X should always remain the same!? 恕我直言,如果一个和同一个文件X的持续时间应始终保持不变!? Or is a cache technique used by the JVM? 或者是JVM使用的缓存技术?

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance 提前致谢

Horace 贺拉斯

NB: - I am doing the tests local (NO remote, NO http) on my machine. 注意: - 我正在我的机器上进行本地测试(没有远程,没有http)。 - I am using java 6 on Ubuntu 10.04 - 我在Ubuntu 10.04上使用java 6

This is totally normal. 这完全正常。 The JIT compiles methods to native code and optimizes them more heavily as they're more and more heavily used. JIT将方法编译为本机代码,并且随着它们被越来越多地使用而对它们进行更大程度的优化。 (The "constant" your benchmark eventually converges to is the peak of the JIT's optimization capabilities.) (你的基准最终收敛的“常数”是JIT优化能力的顶峰。)

You cannot get good benchmarks in Java without running the method many times before you start timing at all. 如果没有 开始计时之前多次运行该方法, 就无法在Java中获得良好的基准测试。

IMHO if the duration for one and the same file X should always remain the same 恕我直言,如果一个和同一个文件X的持续时间应始终保持不变

Not in the presence of an optimizing just-in-time compiler. 不存在优化的即时编译器。 Among other things it keeps track of how many times a particular method/branch is used, and selectively compiles Java byte codes into native code. 除此之外,它还跟踪特定方法/分支的使用次数,并有选择地将Java字节代码编译为本机代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM