Skip to content

Instantly share code, notes, and snippets.

@regiskuckaertz
Last active November 22, 2024 11:30
Show Gist options
  • Save regiskuckaertz/4fd8c9055b92608c4c9bc0639a760b55 to your computer and use it in GitHub Desktop.
Save regiskuckaertz/4fd8c9055b92608c4c9bc0639a760b55 to your computer and use it in GitHub Desktop.
A scala-cli script for the "1 Billion nested loop iterations" experiment
//> platform native
@scala.annotation.tailrec
def loop(n: Int, i: Int = 0)(f: Int => Any): Unit =
if i < n then
f(i)
loop(n, i + 1)(f)
else ()
@main def run(u: Int) =
val r = scala.util.Random.nextInt(10000)
val a = Array.fill(10000)(0)
loop(10_000) { i =>
loop(100_000) { j =>
a(i) += u
}
a(i) += r
}
println(s"${a(r)}")
@regiskuckaertz
Copy link
Author

Compiled using:

$ scala-cli --power package Index.scala -o Index
Wrote /Users/regiskuckaertz/code/loops/Index, run it with
  ./Index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment