• 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
  On 07/06/2013 at 13:16, Asik said:

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

    • Most notepad alternatives have this option. It's useful imho. In the occasinal crash or windows reboot, your notes (even non saved ones) will pop up again.
    • Streaming was exciting and new in the beginning, nowadays all these services just s#ck big time. Sh#tty shows, cancellations even before a first season has aired, ads, more ads and ever increasing prices. Like I said in a previous post, invest some time setting up a local media server and be way happier. 😉
    • First thing that popped into my head reading this as well.....
    • Windows 11/10 package manager UniGetUI gets massive update with bulk download options by Usama Jawad UniGetUI (formerly WinGetUI) is a considerably popular Windows package manager. It basically combines command-line interfaces from package managers like Chocolatey, pip, npm, and more, in a single intuitive GUI. The project is available on GitHub under the MIT license, sporting almost 17,000 stars and 547 forks, and a decent amount of watchers. Today, UniGetUI has received version 3.3.0. It is interesting to note that this release was actually meant to be version 3.2.1, following on from a respectable 3.2.0 release back in May. But since it features a massive change log with more changes than originally planned, the developer decided to bump it up to version 3.3.0 directly. The highlights from the latest release include an option to bulk-download installers, select a package manager executable, default install options for each package manager, and cloud backup and restore functionalities. In addition, more commands are now supported, and users have the ability to kill processes before installing, uninstalling, or updating a package. There are lots of other capabilities in tow, too. PowerShell7 now clears older versions when updating, the Searchbox has been moved to improve space utilization, the toolbar has been enhanced, internal error detection mechanisms have been upgraded, and some dialog boxes have netted updates. There are tons of other backend changes too, including a note that XAML and YAML files cannot be created anymore due to low usage - the developer says that these formats are only used by 0.7-1.3% of all bundles, so it's difficult to maintain the development overhead required to manage them. As expected, there are lots of fixes contributed by the very active UniGetUI community, too. You can find out the full details in the massive change log here. You can download UniGetUI version 3.3.0 stable from the Neowin software stories page or from its official GitHub repo linked above.
    • Sounds useful. It isn't reading your messages and sending them to Google ffs.
  • Recent Achievements

    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
    • One Month Later
      Ian_ earned a badge
      One Month Later
    • Dedicated
      MacDaddyAz earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      504
    2. 2
      ATLien_0
      207
    3. 3
      Michael Scrip
      205
    4. 4
      Xenon
      141
    5. 5
      +FloatingFatMan
      116
  • Tell a friend

    Love Neowin? Tell a friend!