• Post Reply Bookmark Topic Watch Topic
  • New Topic

with is faster: an if () or a double division?  RSS feed

 
bengt hammarlund
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a value object that stores a double value.
I want it to print and expose it's internal double value using the expression:
Math.round(amount * 100d) / 100d.

So a value like 1023.569 would print 1023.57.
Since I use this same operation to compare objects, and in other places, I an thinking about having a double reference to it's "rounded value". This reference would be initiated only when the first need apears.

I'm thinking of a small otimization like:


If someone has any idea if this "if" instruction cost little enougth so this optimization is really an optimization?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bengt,
It is unlikely that just doing the division would be a performance bottleneck. The first step is to see where the majority of time is being spent. I wouldn't expect it to be there.

Having said that, you could run a test by calling your method in a loop thousands of times to see if there is any performance difference.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well - I expected that 1000 times is much to small on an actual machine to produce a significant difference.
My test showes no measurable difference for 100,000 calls, every call a cache-hit.
For 1,000,000 calls the difference gets significant, but keep in mind: every call a cache-hit.
Do you expect so many compareTo-calls?
What might be the ratio of hit/misses?
How fast are the machines that run the program?

I testet on an i686-1,100 Mhz.
- jdk1.4/ 1.5,
- server/ no server flag.
Both switches without significant difference.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bottom line is, the optimization only has even a slight advantage in very specific cases, but *always* significantly complicates the code and therefore makes maintenance more costly and opens the door for subtle bugs.

So only do this if you:

- have a proven performance problem,
- have proven that this is the main bottleneck, and
- can prove that the optimization removes the bottleneck.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!