0

my Java algorithm is spending 30% of its time calculating the expression Math.pow(10, (0.1x+1.5)) for many different x values (I cannot know these x's beforehand). Is there any way/trick to lower this bottleneck?

jkr
  • 17,119
  • 2
  • 42
  • 68
Gouz
  • 336
  • 1
  • 6
  • 19
  • That's `pow(a, x) * b` where `a = pow(10, 0.1)` and `b = 10 * sqrt(10)` which may be a bit faster though not by much. – dxiv Apr 03 '21 at 21:37
  • 3
    What kind of algorithm? There may be special opportunities that arise thanks to some special context in which that power appears – harold Apr 03 '21 at 21:47
  • [Optimizations for pow() with const non-integer exponent?](https://stackoverflow.com/q/6475373/995714) – phuclv Apr 04 '21 at 01:32
  • `pow` and `exp` can be vectorized and be computed faster if you know that `x` is a normalized finite number (so not `NaN` or `Inf` or even `-0`) assuming the machine use a standard IEEE-754 representation for floating-point numbers. Such kind of trick is not portable (and not well suited to Java). – Jérôme Richard Apr 04 '21 at 01:33
  • I'm also curious to know what you are working on. – Robert Dodier Apr 04 '21 at 02:56

1 Answers1

2

An other option is:

31.6227766017 * Math.exp(0.23025850929 * x)

This is likely faster, because exp is simpler than pow, but I did not test how big the difference (if any) is.

harold
  • 61,398
  • 6
  • 86
  • 164