本文介绍了Java 分析:私有属性 Getter 的基本时间较长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TPTP 来分析一些运行缓慢的 Java 代码,并且我遇到了一些有趣的事情.我的一个私有属性 getter 在 Execution Time Analysis 结果中具有很大的 Base Time 值.公平地说,这个属性被调用了很多次,但我从没想到像这样的属性会花费很长时间:

I'm using TPTP to profile some slow running Java code an I came across something interesting. One of my private property getters has a large Base Time value in the Execution Time Analysis results. To be fair, this property is called many many times, but I never would have guessed a property like this would take very long:

public class MyClass{
    private int m_myValue;
    public int GetMyValue(){
        return m_myValue;
    }
}

好的,显然这个类中有更多的东西,但是正如你所看到的,当调用 getter 时没有其他事情发生(只返回一个 int).一些数字给你:

Ok so there's obviously more stuff in the class, but as you can see there is nothing else happening when the getter is called (just return an int). Some numbers for you:

  • 大约 30% 的运行调用是在吸气剂上(我正在努力减少这)
  • 大约 25% 的基准时间运行在这个 getter 中花费
  • 平均基准时间为 0.000175 秒

为了比较,我在不同的类中有另一个使用这个 getter 的方法:

For comparison, I have another method in a different class that uses this getter:

private boolean FasterMethod(MyClass instance, int value){
    return instance.GetMyValue() > m_localInt - value;
}

平均基准时间低得多,为 0.000018 秒(低一个数量级).

Which has a much lower average base time of 0.000018s (one order of magnitude lower).

这里有什么交易?我认为有一些我不明白或我遗漏的东西:

What's the deal here? I assume there is something that I don't understand or something I'm missing:

  1. 返回本地原语真的比返回计算值花费的时间更长吗?
  2. 我是否应该查看基准时间以外的指标?
  3. 这些结果是否具有误导性,我需要考虑使用其他一些分析工具吗?

编辑 1:根据下面的一些建议,我将该方法标记为最终方法并重新运行测试,但我得到了相同的结果.

Edit 1: Based on some suggestions below, I marked the method as final and re-ran the test, but I got the same results.

编辑 2:我安装了一个演示版的 YourKit 来重新运行我的性能测试,YourKit 结果看起来更接近我的预期.我将继续测试 YourKit 并报告我的发现.

Edit 2: I installed a demo version of YourKit to re-run my performance tests, and the YourKit results look much closer to what I was expecting. I will continue to test YourKit and report back what I find.

编辑 3: 更改为 YourKit 似乎解决了我的问题.我能够使用 YourKit 来确定代码中实际的慢点.下面有一些很棒的评论和帖子(适当地投票),但我接受第一个建议 YourKit 为正确"的人.(我不以任何方式隶属于 YourKit/YMMV)

Edit 3: Changing to YourKit seems to have resolved my issue. I was able to use YourKit to determine the actual slow points in my code. There are some excellent comments and posts below (upvoted appropriately), but I'm accepting the first person to suggest YourKit as "correct." (I am not affiliated with YourKit in any way / YMMV)

推荐答案

这听起来很奇怪.您不会错误地调用了覆盖方法,是吗?

That sounds very peculiar. You're not calling an overriding method by mistake, are you ?

我很想下载YourKit 的演示版.设置起来很简单,它应该表明真正发生了什么.如果 TPTP 和 YourKit 都同意,那么就会发生一些奇怪的事情(我知道这没什么帮助!)

I would be tempted to download a demo version of YourKit. It's trivial to set up, and it should give an indication as to what's really occurring. If both TPTP and YourKit agree, then something peculiar is happening (and I know that's not a lot of help!)

这篇关于Java 分析:私有属性 Getter 的基本时间较长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 11:47