Author Topic: sin/cos function vs lookup table  (Read 4079 times)

Offline LJ

  • Retired Staff
  • Posts: 1661
  • Cookies: 1139
sin/cos function vs lookup table
« on: February 22, 2007, 07:26:39 PM »
Hello people - I was wondering if I could have some opinions on an idea for an optimisation on the physics engine in Immersion.
Currently I rotate my objects with your bog standard z rotation matrix.  However this requires at the very least, a sin and a cos call.  These are _very_ expensive and I want to avoid them.  I have a couple of ideas on how to get round this.

One is to keep a lookup table of sins and cosines for each degree of rotation.   I realise it would give me a choppy sine/cos wave but i don't care really because there should't be any done with a resolution of smaller than one degree. 

So what i would like is opinions on is the speed of a lookup in a python dictionary vs. a sine, cosine and a *0.01745... call.

Thanks :mrgreen:


LJ


here is my current call for a 2D rotation about the Z axis
Code: [Select]
        ## |X| | cos(a) -sin(a) ||X|
        ## |Y| | sin(a) cos(a) ||Y|
        fRadians = fDegrees*0.0174532925
        fx = self.oForward.x
        fy = self.oForward.y
        c = math.cos(fRadians)
        s = math.sin(fRadians)
        x = c * fx - s * fy
        y = s * fx + c * fy
        self.oForward.x = x
        self.oForward.y = y

Offline RCgothic

  • Retired Staff
  • Posts: 428
  • Cookies: 51
Re: sin/cos function vs lookup table
« Reply #1 on: February 23, 2007, 05:30:01 AM »
Even with a look up table, you should be able to interpolate to a finer resolution.It wouldn't produce absolutely perfect accuracy, but it would allow smooth rotation.

The impression I get in a quick search in google is that lookup + interpolation is pretty common. It wouldn't be common if it wasn't faster.

Offline MLeo

  • Retired Staff
  • Posts: 3636
  • Cookies: 833
  • Software Simian
    • the Programming Pantheon
Re: sin/cos function vs lookup table
« Reply #2 on: February 23, 2007, 05:50:54 AM »
As RCGothic said, lookup+interpolation is one of the faster results.
Some of the CPU specific assembly calls for sincos uses lookup tables.

IMO, I'm unsure if a python dictionary is the best choice for this.
Floating point issues could arise from the use of it. I mean, the look up could fail over this.

If you are going to do use degree's as the basis, then a d%360 lookup to a list (the [] variant, it seems more optimised for lookups) could be even faster.


Also, this is very interresting:
http://www.devmaster.net/forums/showthread.php?t=5784

Atleast, for C/C++, but it might also be applicable for Python, it doesn't beat speed for lookup (assuming you do integer lookups), but it's faster than a direct call to sincos and more accurate than a look up (I think).
I still can't read peoples minds, nor can I read peoples computers, even worse, I can't combine the two to read what is going wrong with your BC install...

"It was filed under 'B' for blackmail." - Morse, Inspector Morse - The dead of Jericho.

Offline Defiant

  • Posts: 398
  • Cookies: 1105
    • BC: Kobayashi Maru
Re: sin/cos function vs lookup table
« Reply #3 on: March 14, 2007, 08:37:13 AM »
censored

Offline LJ

  • Retired Staff
  • Posts: 1661
  • Cookies: 1139
Re: sin/cos function vs lookup table
« Reply #4 on: March 17, 2007, 11:41:02 AM »
Code: [Select]
c func: 2.864436s
lookup: 1.045019s
taylor: 6.341741s
same sorta thing over here :)  getting a good second's increase which is great.  Thankyou everyone! :mrgreen:

wz

  • Guest
Re: sin/cos function vs lookup table
« Reply #5 on: May 26, 2007, 07:00:44 AM »
A very fast method for both sin and cos computation is the one described in this document: http://www.dspguru.com/comp.dsp/tricks/alg/sincos.htm

HTH,

wz