5 ms·
I guess it depends on your team. We didn't have much trouble with Java guys picking up Scala very quickly. Scala performance is on par with Java if you keep yo
by trailfox 13y ago
I guess it depends on your team. We didn't have much trouble with Java guys picking up Scala very quickly.
Scala performance is on par with Java if you keep your wits about you, but you do have to be mindful of the code you write. Scala collections (esp. maps, doubly so for immutable.Map, but also mutable.Map) are (currently) slower than the standard Java collections, but it's easy to use an implicit conversion wrapping a Java HashMap with little to no overhead in the very small number of situations where this type of thing is a bottleneck. For comprehensions can also cause some overhead, so if you're really pressed for performance a while loops is always an option.
If anyone from typesafe/Scala is reading this: please bring the performance of mutable.Map on par with java.util.HashMap
- mark242 13y agoIf map performance is really an issue for you, you should look into using the parallel collections. For maps with less than 10k entries, I'm unable to see any difference between the immutable/mutable and ju.HashMap collections. For maps with >100k entries, it makes sense to farm out your iterating to multiple threads.
- trailfox 13y agoPut and lookup are much slower vs ju.HashMap. For most use cases where the system is already multithreaded the parallel collections don't gain us anything. If you have a large read-only map being read by multiple threads (doing random lookups) ju.HashMap leaves both mutable.Map and immutable.Map very far behind. Scala is great, but the Map performance lags. I'm not the only one who has noticed. Try googling "Scala map performance vs Java"
- psuter 13y agoFor some use cases (not necessarily yours), it's worth looking at Scala's concurrent.TrieMap.
- orp 13y agoYou should take a look at scala's OpenHashMap - a mutable hashmap in the scala standard library that is often as fast as ju.HashMap. Link to scala doc: http://www.scala-lang.org/api/current/index.html#scala.collection.mutable.OpenHashMap http://www.scala-lang.org/api/current/index.html#scala.colle...
- trailfox 13y agoI have used OpenHashMap, but the results are mixed sometimes is much faster, other times it's much slower. It really depends on the use case.
- bad_user 13y agojava.util.HashMap is not thread-safe, so using it in the context of multiple threads reading from it is less than ideal. The great thing about Scala's immutable HashMap is that in terms of concurrency it is worry-free. You couple it with an AtomicReference and presto, you've got a non-blocking, concurrent HashMap. I work on a really high-volume web service that's built on top of Scala. The performance of Scala's immutable collections has been the least of my worries.
- trailfox 13y agoScala's immutable hashmap is about 4-5x slower than ju.HashMap for reads and 6-7x slower for inserts.
- bad_user 13y ago> For comprehensions can also cause some overhead, so if you're really pressed for performance a while loops is always an option. In most cases in which I need a while loop, I use a tail recursion instead. You can keep your code free of side-effects that way and the compiler generates code equivalent to a while loop.
- aktau 13y ago> In most cases in which I need a while loop, I use a tail recursion instead. You can keep your code free of side-effects that way and the compiler generates code equivalent to a while loop. Hear, hear! I can corroborate this statement and have benchmarked it quite a bit in the past. If you can mark a method as @tailrec in scala and the compiler does not complain, it will go as fast as if it were written with a while loop. And in many cases it will feel cleaner as well. Of course, not everything writes itself nicely in a recursive style, for this you should use while if you need the performance. Don't be afraid, scala is after all a mariage between OO, functional and bits and pieces of everything nice. It's still there for a reason. * the @tailrec attribute is not necessary but forces the compiler to complain when your recursive method is not, in fact, tail recursive. It's also good documentation that a developer who makes changes should keep it tail recursive because it's sensitive.