0

Does java handles the calculations for both versions in the same manner:

version 1:

double output = 5 + 3 + (5 / 3) - 8 * 10 + 12 - 20 - (5 / 3) - 8 * 10 * Math.Pi;

version 2:

double pi = Math.Pi;
double a = 5/3;
double b = 8 * 10;
double output = 5 + 3 + a - b + 12 - 20 - a - b * pi;

in relation to performance/memory/precision are both equal?

"one single operation" means one single rounding? Instead of three seperate roundings for each term??

nimo23
  • 5,170
  • 10
  • 46
  • 75
  • 4
    They're not the same: version 1 is done entirely in integer arithmetic. And also, version 1 is a compile-time constant expression, so it will be evaluated at compile time. – Andy Turner Nov 23 '17 at 22:25
  • I added Math.Pi, because I want to know if there is a difference when using terms or not. – nimo23 Nov 23 '17 at 22:27
  • compile time: means faster, preciser and less memory? – nimo23 Nov 23 '17 at 22:28
  • 1
    There is a difference in the evaluation of these, but there is practically no difference in speed. There is simply no way this is the biggest performance bottleneck in your code. Write your code in the most understandable way, and focus on more important problems. – Andy Turner Nov 23 '17 at 22:28
  • evaluation: is this also related to precision? – nimo23 Nov 23 '17 at 22:29
  • The difference is so small, if there is any, that you should prefer **readability** over performance. Avoid [micro optimizations](https://softwareengineering.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding) (or know when they could matter). – Zabuzard Nov 23 '17 at 22:31
  • I had a very large formula which I must refactor. The other said, I should leave it because it is faster and better. But it s ugly: dozen terms equal, so I want to substitute them to make it easier to maintan. So it does not have any negative impact in precison or the like? – nimo23 Nov 23 '17 at 22:32
  • "does not have any negative impact in precison" you've got the code for the two versions, so just try it. – Andy Turner Nov 23 '17 at 22:32
  • 1
    Definitely go for the more readable and maintainable version. If you substitute correct the precision will be the same (do equivalent transformations). If you think you need to optimize then **first measure** the performance and compare the versions. Only if you see a notifiable difference you should optimize (be aware of [micro-benchmarks](https://stackoverflow.com/questions/2842695/what-is-microbenchmarking)). – Zabuzard Nov 23 '17 at 22:33
  • no the other refactored version does not exist yet. – nimo23 Nov 23 '17 at 22:34
  • "does not exist yet" So implement it, and try it. – Andy Turner Nov 23 '17 at 22:34
  • the first version is one single operation as mentioned before Version 1: ldc2_w [67] 3 dstore_1 [output] Version 2 are three seperate operations Version 2: 0 dconst_1 1 dstore_1 [a] 2 ldc2_w [71] 5 dstore_3 [b] 6 ldc2_w [67] 9 dstore 5 [output] – IEE1394 Nov 23 '17 at 22:38
  • So: "one single operation" means one single rounding? Instead of three seperate roundings for each term?? If so, this is important to know. – nimo23 Nov 23 '17 at 22:42
  • 1
    no rounding at runtime .. its like @AndyTurner said evaluated at compile time. there are only constant expression. but version 2 have 3 instead of version 1 with only one – IEE1394 Nov 23 '17 at 22:45

0 Answers0