Very slow while loop when condition requires value outide of block.
See original GitHub issueCompiler version
3.0.0
Minimized example
object Main extends App {
def timeMeasure[R](name: String)(block: => R): R = {
val startTime = System.currentTimeMillis()
val result = block
val endTime = System.currentTimeMillis()
println(s"Execute '$name' in ${endTime - startTime} ms")
result
}
timeMeasure("filling array via while, n inside of block") {
val n = 1000000
val array = new Array[Int](n)
var i = 0
while (i < n) {
array(i) = i
i += 1
}
array
}
val n = 1000000
timeMeasure("filling array via while, n outside of block") {
val array = new Array[Int](n)
var i = 0
while (i < n) {
array(i) = i
i += 1
}
array
}
}
Output
Execute 'filling array via while, n inside of block' in 4 ms
Execute 'filling array via while, n outside of block' in 7497 ms
Event when I warm
my JVM, and execute this test 100 times, result (I mean, performance) the same.
Expectation
I expect that 2nd block will executes in about 4 ms. I found that there is difference in generated Java bytecode: 2nd version use n() as function. But when I copy this code as Java code, and run it, it was fast. So problem not only in using n as function instead of using it as integer. This problem also exists on Scala 3.0.1 and event on 3.1.2-RC1-bin-20211022-f7abd32-NIGHTLY.
Linked with: https://stackoverflow.com/questions/69697784/why-fill-array-in-while-loop-is-so-slow-in-scala-3
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Cannot use variable in while clause that is declared inside the ...
The condition inside the while loop is outside of the block in which the variable is defined. int composite; do { index +=...
Read more >DataCamp_-_Track_-_Data_Scientist_with_R_-_Course_02_ ...
The condition of the while loop should check if speed is higher than 30. Inside the body of the while loop, print out...
Read more >Apply for each is very slow - Power Platform Community
Hello, I am using Apply for Each to loop through the records and some conditions in LOOP. My observation is that the Flow...
Read more >Understanding While loops in SQL Server - SQLShack
The while loop in SQL begins with the WHILE keyword followed by the condition which returns a Boolean value i.e. True or False....
Read more >Python "while" Loops (Indefinite Iteration)
The while Loop; The Python break and continue Statements; The else Clause ... the designated block is executed repeatedly as long as some...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
in Scala 2,
App
usesDelayedInit
, as per https://docs.scala-lang.org/scala3/book/methods-main-methods.html . the doc page should probably call attention to the slow-constructor problem, since it’s the whole reasonDelayedInit
existsAnd it seems like using
@main
with a top level val has the same speed: