Note that there are some explanatory texts on larger screens.

plurals
  1. POScala 2.8 vs 2.9 benchmark, strange results
    text
    copied!<p>Some time ago, I wrote some code to decide which method of updating mutable variable with new value (but without creating new objects) is faster. One method uses a temporary value and there is an extra copying, and other does not.</p> <p><em>code sample #1 - with reassignment</em></p> <pre><code>class Test(var x: Float, var y: Float) { @inline final def :=(x: Float, y: Float) = { this.x = x this.y = y } //... @inline final def :=(o: Test) = { //reassign from arg "o" x = o.x y = o.y } //... } object Benchmark { //... val tmp = new Test(0, 0) @inline final def calc_something_for_reassign(a: Float, b: Float) = { tmp := (a, b) tmp.something_m //this is a simple method that changes the object tmp } //... } </code></pre> <p><em>code sample #2 - without reassignment</em></p> <pre><code>class Test(var x: Float, var y: Float) { @inline final def := (x: Float, y: Float) = //it's the same as in sample #1 //... @inline final def := (of: (Test) =&gt; Unit) = { //apply mutating function "of" of(this) } //... } object Benchmark { //... @inline final def calc_something_for_forwarding(a: Float, b: Float) = { (result: Test) =&gt; { result := (a, b) result.something_m } } } </code></pre> <h2>Full code is here: <a href="http://ideone.com/A62Ts" rel="nofollow">http://ideone.com/A62Ts</a></h2> <p>On my PC (scala compiler v. 2.8, jre7) the results looks like expected:</p> <pre><code>reassignment: 0.046sec; Result:42.0, 3.0 forwarding: 0.007sec; Result:42.0, 3.0 forwarding: 0.006sec; Result:42.0, 3.0 reassignment: 0.044sec; Result:42.0, 3.0 </code></pre> <p>(Yes, if I increase test length, the ratio is the same, and compiler options don't matter.)</p> <p>I executed this test on a cell phone with Android OS 2.3.4 and winner is still the same. But, as we can see, on Ideone.com opposite things happen, second method is much slower. So can you explain, what is actually happening there? And which (of these two) version of Scala compiler is working right? Is it a bug in scala-2.9.1?</p> <p>UPDATE: on scala 2.9 test prints the following (tried many times, nothing changes too much):</p> <pre><code>reassignment: 0.047sec; Result:42.0, 3.0 forwarding: 0.032sec; Result:42.0, 3.0 forwarding: 0.219sec; Result:42.0, 3.0 reassignment: 0.045sec; Result:42.0, 3.0 </code></pre> <p>And with any number of cycles the third run is longer than all others. May it cause GC? Or why else such weird thing can happen? If I instead call them in order <em>b, b, a, b, b, a</em>, then there's no such strange peaks:</p> <pre><code>println(Benchmark.run_b(Int.MaxValue/10)) println(Benchmark.run_b(Int.MaxValue/10)) println(Benchmark.run_a(Int.MaxValue/10)) println(Benchmark.run_b(Int.MaxValue/10)) println(Benchmark.run_b(Int.MaxValue/10)) println(Benchmark.run_a(Int.MaxValue/10)) </code></pre> <p><em>Results in scala 2.8 (stable):</em></p> <pre><code>forwarding: 0.012sec; Result:42.0, 3.0 forwarding: 0.012sec; Result:42.0, 3.0 reassignment: 0.347sec; Result:42.0, 3.0 forwarding: 0.011sec; Result:42.0, 3.0 forwarding: 0.005sec; Result:42.0, 3.0 reassignment: 0.333sec; Result:42.0, 3.0 </code></pre> <p><em>Results in scala 2.9 (stable):</em></p> <pre><code>forwarding: 0.184sec; Result:42.0, 3.0 forwarding: 0.179sec; Result:42.0, 3.0 reassignment: 0.354sec; Result:42.0, 3.0 forwarding: 0.17sec; Result:42.0, 3.0 forwarding: 0.169sec; Result:42.0, 3.0 reassignment: 0.342sec; Result:42.0, 3.0 </code></pre> <p>However, the method with reassignment in scala 2.9 is equally long as in scala 2.8, <strong>but fast method is about 10x slower in scala 2.9 (but still about 2 times faster than slow method). So the original question is still alive, why it's so much slower in scala 2.9.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload