Created
August 19, 2016 20:45
-
-
Save lucaswerkmeister/cfaa1b493962f1f3905a78bb17b9fca2 to your computer and use it in GitHub Desktop.
HotSpot boxing, Ceylon, anomaly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang { System { o=\iout, nanoTime }, Double, Long } | |
Integer additions = 10_000_000_000; | |
String format = "%04d. iteration, %8s: %04.1f seconds (%12d ns): %d\n"; | |
Float seconds(Integer t1, Integer t2) => (t2 - t1) / 1.0e9; | |
Integer nanos(Integer t1, Integer t2) => t2 - t1; | |
class C() { | |
@unboxed variable Integer i1 = 0; | |
@boxed variable Integer i2 = 0; | |
shared void addU(@unboxed Integer i) { | |
i1 += i; | |
} | |
shared void addB(@boxed Integer i) { | |
i2 += i; | |
} | |
shared [Integer, Integer] result => [i1, i2]; | |
} | |
shared void run() { | |
for (iteration in 0:10) { | |
value c = C(); | |
value t3 = nanoTime(); | |
for (x in 0:additions) { | |
c.addB(x); | |
} | |
value t4 = nanoTime(); | |
o.printf(format, Long(iteration), "boxed", Double(seconds(t3, t4)), Long(nanos(t3, t4)), Long(c.result[1])); | |
value t1 = nanoTime(); | |
for (x in 0:additions) { | |
c.addU(x); | |
} | |
value t2 = nanoTime(); | |
o.printf(format, Long(iteration), "unboxed", Double(seconds(t1, t2)), Long(nanos(t1, t2)), Long(c.result[0])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment