Results 1 to 17 of 17

Thread: [VB.NET 4.0] Can you speed up this function? :)

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    183

    [VB.NET 4.0] Can you speed up this function? :)

    I have some code for calculating a "polytrope" (a good model for some types of star). I was reading some optimisation threads on this forum today and it made me curious enough to try to make it faster. It was already probably the most efficient program I'd made since it was my first truly multithreaded application (that actually benefited hugely from it).

    Moving "Math.PI" into a constant so it's only called once didn't make any measurable difference in the time taken. However, replacing two occurrences of "r ^ 2" with a new variable initialised to "r ^ 2", which was then used twice, improved the speed of the code by 35%!

    I don't actually need this code as it's from an old program but I'm just curious to see if there are any other efficiency improvements one could make?

    P.S. This is running in 64-bit mode so I don't think changing any of the Doubles into Singles will improve speed.

    VB Code:
    1. Public Const G As Double = 0.00000000006674     'gravitational constant
    2.     Public Const Pi As Double = Math.PI             'Pi
    3.  
    4.     Public Function CalculatePolytrope(ByVal n As Double, ByVal StepSize As Double, ByVal Rho As Double, ByVal P As Double) As Double()
    5.  
    6.         Dim K As Double = P / (Rho ^ ((n + 1) / n)) 'constant of polytrope model
    7.         Dim m As Double = 0                         'mass
    8.         Dim r As Double = 0 - StepSize              'radius
    9.         Dim dP_dr As Double                         'dP/dr
    10.         Dim dm_dr As Double                         'dm/dr
    11.         Dim rSquared As Double                      'r^2
    12.  
    13.         'Process loop until Rho or P are equal to (or less than) zero:
    14.         Do
    15.             'Increment r
    16.             r += StepSize
    17.  
    18.             'This prevents dP/dr and dm/dr becoming NaN ("not a number") when r = 0
    19.             If r = 0 Then
    20.                 dP_dr = 0
    21.                 dm_dr = 0
    22.             Else
    23.                 rSquared = r ^ 2
    24.                 dP_dr = -(G * m * Rho) / rSquared
    25.                 dm_dr = 4 * Pi * Rho * rSquared
    26.             End If
    27.  
    28.             'Calculate new mass and pressure
    29.             m += StepSize * dm_dr
    30.             P += StepSize * dP_dr
    31.  
    32.             'This prevents square rooting a minus number, which would make Rho NaN ("not a number")
    33.             If P > 0 Then
    34.                 Rho = (P / K) ^ (n / (n + 1))
    35.             Else
    36.                 Rho = 0
    37.             End If
    38.         Loop Until Rho <= 0 OrElse P <= 0
    39.  
    40.         'Create an array of doubles to hold our final M and R then return them to the calling subroutine.
    41.         Dim Results() As Double = {m, r}
    42.         Return Results
    43.  
    44.     End Function
    Last edited by DragonQ; Feb 2nd, 2011 at 09:08 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width