question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Very slow while loop when condition requires value outide of block.

See original GitHub issue

Compiler 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:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
SethTisuecommented, Oct 26, 2021

in Scala 2, App uses DelayedInit, 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 reason DelayedInit exists

0reactions
dwijnandcommented, Oct 26, 2021

The previous functionality of App, which relied on the “magic” DelayedInit trait, is no longer available. App still exists in limited form for now, but it doesn’t support command line arguments and will be deprecated in the future.

And it seems like using @main with a top level val has the same speed:

$ scala -3.head i13819.scala
Execute 'filling array via while, n inside of block' in 4 ms
Execute 'filling array via while, n outside of block' in 3 ms
Execute 'filling array via while, m top level' in 4 ms
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found