- About Scala
- In the Enterprise
- Scala Community
- Language Research
- In the Press
- The Scala Team
- Scala's Prehistory
- Contact Us
- Learning Scala
- Tour of Scala
- Scala API
- Setup & Getting Started
- Programming Guides
- Other Guides
- Code Examples
- Scala Developers
2.8+ and while versus for-comprehensions
Wed, 2010-02-10, 00:09
I read a lot of posts lately about the speed differences between
plain-and-ugly while loops to replace equivalent but much slower for (i <-
low until high) constructs.
Today I rewrote a genetic algorithm from Java to Scala. I purposely used
for-comprehensions instead of while loops to see how big the difference
would be. Because most loops in the algorithm are pair-wise operations on
two arrays foreach is not an efficient alternative.
As could be expected the Scala versions was about 4-5 times slower than the
Java version. Then I replaced the for-comprehensions with equivalent while
loops and that brought the Scala version within 2-3% of the Java version.
Although I understand what causes this I am curious if future Scala versions
might be able to close this gap. The while-based code is much less readable
than the for-version and arguably even less pretty than the original Java
version which is in total contrast with all previous experiences rewriting
Java stuff in Scala.
Any chance future Scala versions may be able to specialize plain integer
versions of for-comprehensions?