Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8140469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:10:40+00:00 2026-06-06T12:10:40+00:00

Some time ago, I wrote some code to decide which method of updating mutable

  • 0

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.

code sample #1 – with reassignment

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 sample #2 – without reassignment

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) => Unit) = { //apply mutating function "of"
        of(this)
    }
//...
}
object Benchmark {
    //...
    @inline final def calc_something_for_forwarding(a: Float, b: Float) = {
        (result: Test) => {
            result := (a, b)
            result.something_m
        }
    }
}

Full code is here: http://ideone.com/A62Ts

On my PC (scala compiler v. 2.8, jre7) the results looks like expected:

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

(Yes, if I increase test length, the ratio is the same, and compiler options don’t matter.)

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?

UPDATE: on scala 2.9 test prints the following (tried many times, nothing changes too much):

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

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 b, b, a, b, b, a, then there’s no such strange peaks:

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))

Results in scala 2.8 (stable):

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

Results in scala 2.9 (stable):

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

However, the method with reassignment in scala 2.9 is equally long as in scala 2.8, 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:10:42+00:00Added an answer on June 6, 2026 at 12:10 pm

    For one thing, the ideone runtime will be memory resident and the JVM may have some stats skewed by other users which has caused it to construct some slightly bizarre native code. I’d be tempted to ignore anything that comes out of it performance wise as there’s so many things that are unknown which affect benchmarks. It’s even possible they’ve got a modified version to suit their needs more specifically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a function I wrote quite some time ago that works fine, but
Some time ago, I've got a new single board computer running Debian which will
So I'm working with some older code I wrote some time ago. I was
I was tidying up some code I wrote a long time ago, putting it
Some time ago, I wrote a small app, which needs to create outbound phone
I was revisting some code I wrote a long time ago, and decided to
Some time ago I wrote a little piece of code to ask about on
Some time ago I wrote an Eclipse plugin which makes use of JDT to
I wrote a code sometime ago in which the object of ResultSet class res
Some time ago I created this LAMP based web. At the time I wrote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.