Jump to content



Photo
java varables

  • Please log in to reply
2 replies to this topic

#1 SirEvan

SirEvan

    Neowinian Senior

  • Joined: 17-April 03
  • Location: Santa Clara, CA
  • OS: Windows 8
  • Phone: HTC One (AT&T)

Posted 07 June 2013 - 03:06

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

Instead of:

final DateTime dt = new DateTime();
final String dayOfTheWeek = String.valueOf(Globals.DayFormat.print(dt));

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 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 07 June 2013 - 13:16

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.

#3 OP SirEvan

SirEvan

    Neowinian Senior

  • Joined: 17-April 03
  • Location: Santa Clara, CA
  • OS: Windows 8
  • Phone: HTC One (AT&T)

Posted 07 June 2013 - 23:30

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.