• 0

Any performance benefits to using object inline instead of declaring a vari


Question

This is probably a really noobish question, but I don't know what to type in google to find the answer.

Is there a benefit performance or memory usage wise by doing the following:

   final String dayOfTheWeek = String.valueOf(Globals.DayFormat.print(new DateTime()));[/CODE]

Instead of:

[CODE]
final DateTime dt = new DateTime();
final String dayOfTheWeek = String.valueOf(Globals.DayFormat.print(dt));[/CODE]

basically saving declaring the first variable and then using it in the second line? Or does it not really matter? I know variable declaration is costly, but not sure if doing the first thing is the equivalent of the second.

2 answers to this question

Recommended Posts

  • 0

If the variable "dt" is used nowhere else than as an argument to that print method, then the compiler is able to see that and reduce case 2 to case 1. It doesn't matter whatsoever. Do what is more readable.

Variable declaration is essentially a no-op; allocating an object in Java amounts to incrementing a pointer and zeroing out the bytes of that object; the only thing that can be arbitrarily costly is the code inside the constructor that is called, and even then, good coding practice is to have constructors never do computationally expensive things. So, in general, variables are practically free and creating objects is very cheap. Don't hesitate to create more variables than necessary to make the code cleaner; the compiler will generate the exact same assembly code in the long run.

As always, you can easily answer questions like this yourself by benchmarking the code, i.e. use some timer and run the piece of code in a loop many thousand times.

  • Like 1
  • 0

If the variable "dt" is used nowhere else than as an argument to that print method, then the compiler is able to see that and reduce case 2 to case 1. It doesn't matter whatsoever. Do what is more readable.

Variable declaration is essentially a no-op; allocating an object in Java amounts to incrementing a pointer and zeroing out the bytes of that object; the only thing that can be arbitrarily costly is the code inside the constructor that is called, and even then, good coding practice is to have constructors never do computationally expensive things. So, in general, variables are practically free and creating objects is very cheap. Don't hesitate to create more variables than necessary to make the code cleaner; the compiler will generate the exact same assembly code in the long run.

As always, you can easily answer questions like this yourself by benchmarking the code, i.e. use some timer and run the piece of code in a loop many thousand times.

Thanks for the confirmation. I already use System.nanoTime() for start and stop, then subtract the two when I want to try benchmarking something, but that's not always accurate due to other things going on on the phone...sometimes i see maybe a few ms difference, which I can't be sure of is really optimizing anything.

This topic is now closed to further replies.
  • Posts

    • low latency mode is still bugged and causing bootup times slow to a crawl. To fix, you have to disable the feature with vivetool. Seems as though it's not rolled out to a lot of people yet since I've only been able to find only a handful of people that are having issues.
    • I would recommend the Nothing 2a. The battery life is awesome, 2 or 3 days without going into battery power mode. The only thing that I've been looking into recently is that it doesn't "support" Graphene OS. I'm pretty sure there is a way, I just need to do some more looking.
    • You'd have to show me an example of a listing that says Gen 1, usually i'd expect that to mean Snapdragon Gen 1 (a type of chipset, which the Pixels don't use). Pixel 7 - White - 128gb - Unlocked - 85%+ battery - Grade B+ - $159 with free delivery - https://www.ebay.com/itm/398046617206 Pixel 7 - Obsidian - 128gb - Unlocked - 80%+ battery - Very Good - $157 with free delivery - https://www.ebay.com/itm/355617734563 Both look to be sold by companies with good feedback, dealing with refurbished phones and state the phones are unlocked with a clean IMEI. Obviously I can't vouch for either company though, but the listings look good in my opinion.
    • Because Chrome is doing it. And no one said anyone had to update immediately. That's silly. They could update every day for all I care as long as it's fast, and the next time the browser restarts, you're good. And the basic point is not to tee it up for bigger updates. As it is right now, all the windows I had open reopen anyway except inprivate.
    • Why? Does anybody actually want this? The constant need to close all browser sessions and wait for a new version to install, just so that there’s a integrated coupon manager feels like a waste of everyone’s time
  • Recent Achievements

    • Week One Done
      davidbazooked earned a badge
      Week One Done
    • One Month Later
      Jamswaz earned a badge
      One Month Later
    • Week One Done
      Jamswaz earned a badge
      Week One Done
    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
  • Popular Contributors

    1. 1
      +primortal
      514
    2. 2
      PsYcHoKiLLa
      185
    3. 3
      +Edouard
      159
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!