On 21.01.2016 16:39, Marshall, Simon wrote:
[...]
For example, on my 3+GHz Xeon with jdk1.8.0_66/groovy-all-2.4.3, the
below code gives me:
t.member = val 34.577
t.setMember(val) 3.529
t.with { member = val } 127.922
*package*test
*import*groovy.transform.CompileStatic
@CompileStatic
*class*Test {
String member
}
String val = 'test'
Test t = *new*Test()
*for*(*long*i = 0; i < 100* 1000* 1000; ++i) {
t.setMember(val)
}
this code does statically compile Test, but not code, that uses test. If
you meassure t.setMember(val) here, it will be the dynamic code you
test, not static compiled code. In other words: the method that does
your field access must be annotated by @CompileStatic.. smae with the
other variants of course. In your code I would actually expect no real
difference in numbers even if you left out the @CompilStatic on Test,
because the setter, does essentially what Java would do (I think there
is no meta class init in there even)
and of course there is the issue of you doing all the tests together. On
the JVM you have something like three states to consider... (1) warmup,
(2) hot (3) deoptimizing.
You usually start with a warmup, get into a hot phase, which might be
followed by an deoptimization, which then again leads to a warmup.
Warmup means the optimizations are applied, the thresholds are crossed.
So if you meassure the warmup, you do not meassure peak performance, you
meassure all the optimization work and running non-optimized code. The
peak performance you get if you meassure only in the hot phase which is
usually done by running the code first several thousands of times (some
thresholds are quite high) and then you do the loop with the actual
meassurement.
Now.. if you do access in a different way, the JVM may have to throw
away optimizations it did before. This can result in significantly
slower code than even in the warmup phase... meanig it makes very much
sense to give the JVM some time to warmup again between meassurements.
If you want to "get rid" of the trouble of doing all those things, you
could consider using JMH, which does not only an arbitrary warmup, it
does even check for the times to get stable.
bye Jochen