• 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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

This topic is now closed to further replies.