-
Notifications
You must be signed in to change notification settings - Fork 21
Description
According to Tiark Rompf, "With -optimize, scalac should compile simple for comprehensions to the equivalent of a while loop. If it does not it's a bug (either in the library or in the compiler) and it needs fixing. So if there is indeed a bug, gather solid performance numbers [and] submit a ticket"
Here are performance numbers for loops nested 1, 3, 5, or a variable number of levels deep; in every case, the innermost operation occurs one billion times. The leftmost number is a checksum (the first three should be the same; the fourth is different):
// These are while loops
-1243309312 While1 Elapsed: 0.314 s
-1243309312 While3 Elapsed: 0.301 s
-1243309312 While5 Elapsed: 0.316 s
-17609343 While? Elapsed: 1.151 s
// These are for loops with ranges
-1243309312 Limit1 Elapsed: 0.923 s
-1243309312 Limit3 Elapsed: 0.885 s
-1243309312 Limit5 Elapsed: 2.527 s
-17609343 Limit? Elapsed: 3.308 s
// These use a C-for-like-method that takes an anonymous function
// Note that the JVM gets confused about how to optimize repeated uses
-1243309312 Cfor1 Elapsed: 9.794 s
-1243309312 Cfor3 Elapsed: 0.324 s
-1243309312 Cfor5 Elapsed: 0.450 s
-17609343 Cfor?? Elapsed: 5.662 s
Code is attached.