https://kotlinlang.org logo
#getting-started
Title
# getting-started
t

thhh

05/02/2021, 2:12 PM
Why does removing "redundant initializer" as suggested by IntelliJ in this code: https://pl.kotl.in/7Kcy18ROA drastically increases the time required to run the program? The
time
variable has a redundant initializer which is commented. When you uncomment and use that redundant initializer, program runs a lot faster. Findings: when using redundant initializer: 20000100000 in 28ms --> which is quite fast. when removing redundant initalizer: 20000100000 in 103ms --> which is slow but is recommended by IntelliJ. IntelliJ hint aside what causes the Kotlin program to run "slower" when NOT using redundant initlializer?
d

diesieben07

05/02/2021, 2:16 PM
Your measurement is flawed because you are not using
result
. The JVM might eliminate the whole computation because it sees that the result is not necessars. Removing the initializer might make that optimization no longer trigger. Proper measurement requires tools such as JMH that have a "black hole" operation that prevents discarding of results
t

thhh

05/02/2021, 2:31 PM
I see, need to look at that. @diesieben07 btw the author of the code (not me) has commented why it was done the way it was done:
we are calling
compute
twice here, we call it the first time to warm up the processor, to warm up Java and second time we call it and we print out, how long this program takes to execute.
This is referring to the
time
variable intilizer. Is this something developers do often and intentionally in other Kotlin code as well?
d

diesieben07

05/02/2021, 2:34 PM
Doing it outside of benchmarks is pretty pointless. But yes, in Benchmarks on the JVM (and all JIT compiled languages) you have to "warm up" the code, otherwise its just running in the interpreter and you are not measuring the true performance.
👍 2
8 Views