简体   繁体   English

Java调用类型性能

[英]Java call type performance

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

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. 如果他们确实具有相同的确切性能,那么这意味着在修改的非最终案例中具有该间接水平没有任何惩罚。

I know standard optimization advice would be: "write your code and profile" but I'm writing a framework code generation kind of thing so there is no specific code to profile, and the choice between static and non final is fairly important for both flexibility and possibly performance. 我知道标准优化建议是:“编写你的代码和配置文件”,但我正在编写一个框架代码生成类的东西,所以没有特定的代码来分析,静态和非最终之间的选择对于两者的灵活性都非常重要并可能表现。 I am using framework code in the microbenchmark which I why I can't include it here. 我在微基准测试中使用框架代码,为什么我不能在这里包含它。

My test was run on Windows JDK 1.7.0_06. 我的测试是在Windows JDK 1.7.0_06上运行的。

If you benchmark it in a tight loop, JVM would cache the instance, so there's no apparent difference. 如果你在紧密循环中对它进行基准测试,JVM会缓存实例,所以没有明显的区别。

If the code is executed in a real application, 如果代码在实际应用程序中执行,

  1. if it's expected to be executed back-to-back very quickly, for example, String.length() used in for(int i=0; i<str.length(); i++){ short_code; } 如果预期它会非常快速地背靠背执行,例如,在for(int i=0; i<str.length(); i++){ short_code; }使用String.length() for(int i=0; i<str.length(); i++){ short_code; } for(int i=0; i<str.length(); i++){ short_code; } , JVM will optimize it, no worries. for(int i=0; i<str.length(); i++){ short_code; } ,JVM将优化它,没有后顾之忧。

  2. if it's executed frequently enough, that the instance is mostly likely in CPU's L1 cache, the extra load of the instance is very fast; 如果它足够频繁地执行,那么实例很可能在CPU的L1缓存中,实例的额外负载非常快; no worries. 别担心。

  3. otherwise, there is a non trivial overhead; 否则,有一个非常重要的开销; but it's executed so infrequently, the overhead is almost impossible to detect among the overall cost of the application. 但它很少执行,在应用程序的总体成本中几乎不可能检测到开销。 no worries. 别担心。

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

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