8

My program uses Math.pow() to compute a relatively large double number to the power of 2. Later on I need to find the square root of a very large double number. The problem is, I have to do this over a 100,000 times and it is taking really long. Is there any alternative that can speed this process up? Thanks

Edit: By large numbers I mean between 1000 to 10000 (So probably not that large in computing terms). And in terms of it taking a long time, it takes about 30 secs to do the function 500 times

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
M9A
  • 3,168
  • 14
  • 51
  • 79
  • you have to do this same operation for 100,000 unique numbers? – Brad Feb 27 '13 at 00:17
  • 5
    Can you define "relatively large", "very large", and "really long"? – asteri Feb 27 '13 at 00:18
  • http://stackoverflow.com/questions/7902418/the-best-alternative-of-math-pow-in-j2me – Brad Feb 27 '13 at 00:20
  • 3
    I think a more comprehensive description of your problem would allow us to better assist you. – arshajii Feb 27 '13 at 00:20
  • @Brad - No not unique. I have a text file of a lot of numbers and it is basically a combination of 2 numbers from that file. As a result, the numbers are not unique but the combinations are i.e the two numbers that are inputted into the math functions are unique – M9A Feb 27 '13 at 00:21
  • I have editted the question to clarify what the terms mean – M9A Feb 27 '13 at 00:23
  • 1
    From your edit, can you tell us exactly what "the function" is? Maybe include some code. – arshajii Feb 27 '13 at 00:25
  • It shouldn't take 30 seconds to perform 100,000 computations. I suspect something else is going on in the code. – asteri Feb 27 '13 at 00:26
  • @Jeff - 5000 computations, not 100,000. 100,000 is the total at the end – M9A Feb 27 '13 at 00:29
  • @A.R.S. - The function is basically the two maths function mentioned – M9A Feb 27 '13 at 00:30
  • 4
    Provide code. It seems extremely unlikely that the numbers you're quoting would take anything near that long. Without actual code, we can't figure out what's actually going on. – Louis Wasserman Feb 27 '13 at 00:31
  • 2
    I can do a million pow() and sqrt() operations on random numbers in 210ms. I suggest that reading the file and converting text to binary is taking a significant fraction of the time. – user207421 Feb 27 '13 at 00:36
  • The performance problem may be due to unnecessary allocation of objects. 1. May be you're creating any object every time function is invoked. Consider whether it is possible to create this object once and then use it. 2. The other way to create unnecessary objects is autoboxing. Try not to use boxed types where primitive types are sufficient. Also look at the example named ['Hideously slow program'](http://www.informit.com/articles/article.aspx?p=1216151&seqNum=5) – naXa stands with Ukraine Nov 29 '13 at 16:16

5 Answers5

10

"The power of 2" is squaring. You'd be better off doing that by multiplying the number by itself.

The library version of sqrt is probably faster than anything you could dig up elsewhere. If you call a C routine, you'll just add overhead from the cross-language call. But do you need accurate square roots, or would a table lookup of approximations do? Do the values repeat a lot, i.e. do you often need to calculate the roots of the same numbers? If so, caching the square roots in a HashMap might be faster than computing them.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
9

You are unlikely to find a better(faster) implementation than the Java Math one. You might have more luck trying to change the way you do the calculations in your algorithm. For example, is there any way you can avoid finding the square root of a huge number?

If this doesn't work, you can try implementing it in a more appropriate language that's meant for fast mathematical computations (something like Matlab).

Otherwise, you can try to optimize this in other areas. Perhaps you can try to cache past results if they are useful later.

Oleksi
  • 12,947
  • 4
  • 56
  • 80
  • Well it involves the Euclidean distance formula so I cant avoid it – M9A Feb 27 '13 at 00:19
  • @Matt9Atkins do you really need the euclidean distance? You could use its square for comparisons... – John Dvorak Feb 27 '13 at 00:20
  • Well there are still options for speeding this up. Do you need the actual distance, or are you just comparing them? If you're just comparing them, you can avoid the square root part. – Oleksi Feb 27 '13 at 00:21
  • @Oleksi I guess what the other answer suggests _is_ a speed improvement over `Math.pow(x, 2)` – John Dvorak Feb 27 '13 at 00:21
  • @Oleksi - I will need the actual computed distances as I will need to compare them at the end – M9A Feb 27 '13 at 00:26
  • 2
    If you need to compare them, then will get the same result if you leave the distances in the not square root form! Note that if x < y, then sqrt(x) < sqrt(y) and vise versa. This can really speed up your program! – Oleksi Feb 27 '13 at 00:28
  • Have you tried `Math.hypot(x, y)` which is designed specifically to solve the Euclidean distance? – Tuupertunut Nov 22 '17 at 00:31
1

Well your problem with the power of 2 can simply be done by multiplying the number by itself. For example, lets say variable a is the number you want to raise to 2. It's the same as: int a=5; int b=a*a;

Mark Beleski
  • 55
  • 1
  • 9
1

the only thing I can think of is storing the results for speed, the square root will not change, and ~9000 stored numbers is not that many. You would probably do well to frame your data, so that you could ensure that you can optimally search for the appropriate result.

0

You can use x*x instead of pow(x, 2).

For square root, you should first take a look on sqrt implementation (approximation method).

Maybe you can find a better one, for example the Newton's method (on equation sqrt(N)-x=0).

It also depends on the needed accuracy, you can trade accuracy vs time.

You can also store results to avoid multiple computations on the same entries.

fso
  • 144
  • 2
  • 14
  • Surely you mean you can use `x*x` instead of `pow(x,2)`. And yes, it is certainly better to calculate it as x*x. – NovaDenizen Feb 27 '13 at 00:29