Code optimisation of math.asin function vb.net

折月煮酒 提交于 2019-12-25 05:59:11

问题


I have the following line of code:

SomeDouble= constant1/ ((a * b) * (Math.Asin((c- a) / (a * d)) + constant2))

The two constants are different and calculated out of the loops, a - d are variables that change each time.

And on the face of it it's pretty fast 0.002ms on average (47,633.588s for 26,508,249 hits). The issue I'm having is it's going to be called billions of times, literally around 20 billion hits each time the software is run. So if I can cut this down to 0.001ms the difference will be substantial. I know that dividing is a very slow process and I expect calculating arcsin is also slow. If anyone can suggest if there's a faster method of calculating arcsin or any other help in speeding up this line of code that would be great. On a side note any advice on whether vb.net's built in math functions are optimised for speed would be great I've noticed that math.sqrt(somevalue) is quicker than (somevalue)^0.5.

Thanks in advanced!


回答1:


I'd do some tests to make sure that the Math.Asin really is the slowest part of the formula. If it really is slow relative to the other multiplications and divisions, then you could try implementing your own lookup table for Math.Asin. In other words, calculate millions of Math.Asin values in advance, and code them into your program. This would trade the size of your program for speed, so if program size doesn't matter, it might help.




回答2:


You can use the following approximation for Asin, in my tests it was just under twice as fast as Math.Asin (32bit, better if manually inlined, under 64bit it was slightly better than twice as fast) and it looked reasonably accurate, but you'd have to test whether the accuracy is acceptable.

static double Asin(double x)
{
    double x2 = x * x;
    double x3 = x2 * x;
    const double piover2 = 1.5707963267948966;
    const double a = 1.5707288;
    const double b = -0.2121144;
    const double c = 0.0742610;
    const double d = -0.0187293;
    return piover2 - Math.Sqrt(1 - x) * (a + b * x + c * x2 + d * x3);[]


}

(this is C# of course, but you can convert it I'm sure)

[EDIT] Here is the VB version.

Shared Function Asin(ByVal x As Double) As Double
    Dim x2 As Double = x * x
    Dim x3 As Double = x2 * x
    Const piover2 As Double = 1.5707963267948966
    Const a As Double = 1.5707288
    Const b As Double = -0.2121144
    Const c As Double = 0.0742610
    Const d As Double = -0.0187293
    Return piover2 - Math.Sqrt(1 - x) * (a + b * x + c * x2 + d * x3)
End Function



回答3:


You could try to implement the series expansion to whatever precision you require, and compare that against Stochastically's suggestion of a lookup table (which may well be how Math.asin ultimately calculates it) built to the precision required. It seems unlikely that you could enjoy the halving of processing time you'd like, however.

If the calculations aren't meaningfully dependent upon one another (or if the dependencies can be isolated into different batches), you might try to run them in parallel (whether on different systems or with different processors), but be wary -- I worked at a space physics lab, and we found that the precision we required generated really annoying anomalies when we ran tests on different systems.

(I also must say, I'm incredibly curious as to why you'd need to run billions of arc-sine calculations.)



来源:https://stackoverflow.com/questions/16740222/code-optimisation-of-math-asin-function-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!