• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ideal way of programming?  RSS feed

 
Anand Satchin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between the below two codes, by means of performace?
Which is the ideal way of programming?

a.
DecimalFormat myFormatter = new DecimalFormat("##########.##");
String output = myFormatter.format(data);
double result = Double.parseDouble(output);

b.
DecimalFormat myFormatter = new DecimalFormat("##########.##");
double result = Double.parseDouble(myFormatter.format(data););
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any performance difference will be completely and utterly negligible. Write the code for clarity, not percevied micro-performance tuning.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's better to use the java.util.Scanner class, in my opinion.
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably wouldn't be able to tell the difference in performance, it would be so small and not worth the effort

Either option works well, depends what you (and others, such as your colleagues) find easiest to view
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first will be easier to debug. Especially chained calls can be very hard to debug. Suppose the following line has a NullPointerException, what could be the cause?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to compile both versions, then view the bytecode with javap -c MyClass. Then see whether there is any difference in the bytecode.

And this question is now beyond "beginning", so I shall move it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It's better to use the java.util.Scanner class, in my opinion.
Hm, for what the original code here is doing, I would think it's better to do some simple math:

I don't know if the question was really supposed to be about the general issue of whether separate variables for intermediate results, or about how to solve the specific problem that the code shown was solving, which appears to be: how to round to two decimal places. Most of the comments have been about the former; this comment addresses the latter.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I see what you mean, Mike S.
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anand,

...maybe you'd like to have a look at this web site
if you are into performance tuning...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!