I put together a microbenchmark that seemed to show that the following types of calls took roughly the same amount of time across many iterations after warmup.
static.method(arg);
static.finalAnonInnerClassInstance.apply(arg);
static.modifiedNonFinalAnonInnerClassInstance.apply(arg);
Run Code Online (Sandbox Code Playgroud)
Has anyone found evidence that these different types of calls in the aggregate will have different performance characteristics? My findings are they don't, but I found that a little surprising (especially knowing the bytecode is quite different for at least the static call) so I want to find if others have any evidence either way.
If they indeed had the same exact performance, then that would mean there was no penalty to having that level of indirection in the modified non final case.
我知道标准优化建议是:"编写你的代码和配置文件",但我正在编写一个框架代码生成类的东西,所以没有特定的代码来分析,静态和非最终之间的选择对于两者的灵活性都相当重要并可能表现.我在微基准测试中使用框架代码,为什么我不能在这里包含它.
我的测试是在Windows JDK 1.7.0_06上运行的.
如果您在紧密循环中对其进行基准测试,JVM 将缓存该实例,因此没有明显的差异。
如果代码在真实的应用程序中执行,
如果预计它会非常快地连续执行,例如String.length()在 中使用for(int i=0; i<str.length(); i++){ short_code; },JVM 会对其进行优化,不用担心。
如果执行得足够频繁,则该实例很可能在CPU的L1缓存中,该实例的额外负载非常快;不用担心。
否则,就会产生不小的开销;但它的执行频率很低,几乎不可能在应用程序的总体成本中检测到开销。不用担心。
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |