- The first call site does not warm up the method. It warms up the call site. "Warmup" is not a proper term fir what you are doing there... The method can be (and likely was) inlined into the call site and optimized there.
- When the second call site is invoked once, it May not have been inlined there yet, and may be calling interpreted or lower tiered optimization versions of the method. If excercized enough, the second call site would likely inline as well.
Separately interesting:
- Your mySleep won't actually do what you think it does. The entire method can be optimized away to nothing after inking at the call site by the JIT once the calls to it actually warm up enough, since it has no side effects and nothing is done with its return code.
- Your while(true) loop in the code and the output don't seem to match. Where is rest of the output? Or is this output from an earlier code version without the while(true) loop?
- You are accumulating more and more contents into an array list. At some point the arraylist will be resized. That does not yet happen in your specific run (1,000,000 iterations plus 1 still fits in the initial 2^20 size), but if you did keep running... if you want to measure while excercizing a large data structure like this, I'd initialize it all and then wrap around it (with some modulu of length) rather than keep accumulating.
And last, calls to the same Java method at different sites *can* end being slower or faster depending on many factors. For example, when inlined at the call sites, the parameters passed may effect the optimizations possible, and may vary depending on call site. More importantly, whether or not a method is inlined is call-site dependent. E.g. some inlining depth or overall calling code size threshold may have been exceeded to stop inlining in one site but not in another.
public static int wasteSomeTime(int t) {
int x = 0;
for(int i = 0; i < t * 10000; i++) {
x += (t ^ x) % 93;
}
return x;
}wasteSomeTime(sleepArg);
So return values demonstrably don't prevent the optimization...
The optimization will not happen if inlining the method at the call site.
I built a small set of jmh benchmarks to demonstrate this. They result in this:
Benchmark (benchLoopCount) (sleepArg) Mode Cnt Score Error Units
MethodInliningExampleBench.noRetValIntLoop 100000 1 thrpt 5 2830940580.903 ± 52900090.474 ops/s
MethodInliningExampleBench.noRetValIntLoopNoInlining 100000 1 thrpt 5 5500.356 ± 245.758 ops/s
MethodInliningExampleBench.retValIntLoop 100000 1 thrpt 5 2877030926.237 ± 134788500.109 ops/s
MethodInliningExampleBench.retValIntLoopNoInlining 100000 1 thrpt 5 0.219 ± 0.007 ops/s
Which differs only in using longs instead of ints.public static long mySleepL1(long t) {
long x = 0;
for(int i = 0; i < t * 10000; i++) {
x += (t ^ x) % 93;
}
return x;
}
Benchmark (benchLoopCount) (sleepArg) Mode Cnt Score Error Units
MethodInliningExampleBench.noRetValLongLoop 100000 1 thrpt 5 2924098828.778 ± 234409260.906 ops/s
MethodInliningExampleBench.noRetValLongLoopNoInlining 100000 1 thrpt 5 0.243 ± 0.013 ops/s
MethodInliningExampleBench.retValLongLoop 100000 1 thrpt 5 0.254 ± 0.014 ops/s
MethodInliningExampleBench.retValLongLoopNoInlining 100000 1 thrpt 5 0.246 ± 0.012 ops/s public static long mySleep(long t) {
long x = 0;
for(int i = 0; i < t * 10000; i++) {
x += System.currentTimeMillis() / System.nanoTime();
}
return x;
}--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.