Scala speed when using get() method on hash tables? (are temporary Option() objects generated?)?

This may happen if you enable escape analysis in the jvm, enabled with.

This may happen if you enable escape analysis in the jvm, enabled with: -XX:+DoEscapeAnalysis on JRE 1.6. Essentially, it should detect objects being created which do not escape the method activation frame and either allocate them on the stack or GC them right after they're no longer needed. One thing you could do is to micro benchmark your code using the scala.testing. Benchmark trait.

Just extend it with a singleton object and implement the run method, compile it and run it. It will run the run method multiple times, and measure execution times.

Didn't know about this method and thought: Cool!. But looking at the source it does not even warm up the JVM. – ziggystar Oct 12 at 10:06 Actually, it does - it runs the benchmark n times, where n is the argument to the main method, and shows all the times.

You can then visually see the point at which the JVM is supposed to be warmed up. – axel22 Oct 12 at 10:43.

Yes, Some objects will be created (None is a singleton). Unless, of course, JVM elides that -- that depends on many factors, including whether or not JVM thinks the code is called all that much. Anyway, that code is not really the standard idiom.

There's even a meme about it: once, one experienced Scala developer was written code like this, when the other one replied "What's this? Amateur hour? Flatmap that sh*t!"

Anyway, here's how I'd rewrite it: ( counts get word map (_. ToDouble / total_tokens * (1.0 - unseen_mass)) getOrElse ( WordDist. Overall_word_probs get word map (unseen_mass * _ / overall_unseen_mass) getOrElse (unseen_mass * WordDist.

Globally_unseen_word_prob / WordDist. Num_unseen_word_types) ) ) You can then refactor this -- both getOrElse parameters could be split in different method with nice names. Since they just return a value without input, they should be pretty fast.

Now, we call just two methods here on Option: map and getOrElse. Here's the beginning of their implementation: @inline final def map @inline final def getOrElse As the parameter to getOrElse is passed by name, it involves an anonymous function creation. And, of course, the parameter to map is also a function.

Other than that, the chance of these methods getting inlined is pretty good. So, here's the refactored code, though I don't know enough about it to give good names. Def knownWordsFrequency = counts get word map computeKnownFrequency def computeKnownFrenquency = (_: Int).

ToDouble / total_tokens * (1.0 - unseen_mass) def probableWordsFrequency = ( WordDist. Overall_word_probs get word map computeProbableFrequency ) def computeProbableFrequency = unseen_mass * (_: Double) / overall_unseen_mass def unknownFrequency = (unseen_mass * WordDist. Globally_unseen_word_prob / WordDist.

Num_unseen_word_types) def estimatedWordsFrequency = probablyWordsFrequency getOrElse unknownFrequency knownWordsFrequency getOrElse estimatedWordsFrequency.

I don't think your example is any kind of standard idiom. The idiom I present is what I've seen in scala2e. I personally find your code nearly incomprehensible -- and I've been programming for about 30 years.

The basic problem is that (IMO) it's completely unobvious in your code that any sort of if-then-else is being performed. – Urban Vagabond Oct 19 at 6:07 The implementation of getOrElse with an anonymous function creation is just horrible (not your fault but Scala's fault) -- very quick way to kill your performance. I noticed this a few days ago when I started disassembling the compiled Scala code.

This is bad bad bad. – Urban Vagabond Oct 19 at 6:09 @UrbanVagabond Try using -optimize to compile, so that getOrElse gets inlined, and the anonymous function disappears. About if-then-else, in my own opinion it shouldn't be there, as it is an implementation detail; getOrElse describes well that either we get the value in one way, or else in another.

And if you think performance of getOrElse is bad, you should see how pattern matching decompiles. Finally... well, let's just say there isn't a single case Some or case None in "Scala in Depth", whereas getOrElse is the second method described. – Daniel C.

Sobral Oct 20 at 23:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions