CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2010
    Posts
    33

    Arc cosine calculation performance problem

    In my previous post I figured out (thanks to D_Drmmr) how to calculate arc cosine with real numbers (including numbers not in the range -1;1).

    Code:
    _acos = -c*log(dotprods[j]+c*pow(one-pow(dotprods[j],2),0.5));
    Anyway my application response times have increased by four times with this line

    Is there any other way to calculate arc cosine (I am using VS2010 in a standard C++ project) faster?

    Or is there any other possible engineering to make the program faster?


    Thanks in advance

  2. #2
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    Re: Arc cosine calculation performance problem

    isnt this equal to:

    Code:
    _acos = -c*log(dotprods[j]+c*sqrt(one-(dotprods[j]*dotprods[j])));

  3. #3
    Join Date
    May 2010
    Posts
    33

    Re: Arc cosine calculation performance problem

    Just a little faster, but they are both slow

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Arc cosine calculation performance problem

    Quote Originally Posted by pm44xl22 View Post
    Is there any other way to calculate arc cosine (I am using VS2010 in a standard C++ project) faster?

    Or is there any other possible engineering to make the program faster?
    Are you testing with a release build? I'm not sure about VS2010, but in previous versions the default project settings did not perform full optimization. Check the project settings, most importantly those under C/C++ --> Optimization.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    May 2010
    Posts
    33

    Re: Arc cosine calculation performance problem

    Yes release mode with optimization at "Favors fast code"

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Arc cosine calculation performance problem

    Code:
    sqrt(one-(dotprods[j]*dotprods[j])));
    If these were dot products of unit vectors, then dotprods[i] == cos(theta), where theta is the angle between the unit vectors.

    In that specific case, this entire expression simplifies to abs(sin(theta)).

  7. #7
    Join Date
    May 2010
    Posts
    33

    Re: Arc cosine calculation performance problem

    I'll try this as solution but the logarithm still takes part. Don't know whether I can get better response times

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Arc cosine calculation performance problem

    calculating a log() is slow. not much you can do about that.

    If you're doing lots of acos calculations, then you can speed things up by making a table of acos over a certain range with a certain step and use interpolation to calculate the value of an acos() call.

    Depending on needed accuracy and range, you may end up needing a very big table though.


    Since you mention 'slow' I'm asuming you're calling acos() thousands/millions of times? You probably need to look at ways to remove the acos() call out of your inner loops rather than trying to optimise the acos() call itself.

  9. #9
    Join Date
    May 2010
    Posts
    33

    Re: Arc cosine calculation performance problem

    Hundreds of thousands, I think you're right and a table is the only way to speed up the process

Tags for this Thread

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

Featured