Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Relative Speed of Trigonometry functions

Relative Speed of Trigonometry functions

Scheduled Pinned Locked Moved Algorithms
performancequestion
15 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MikeMarq

    I'm working on a project which needs to use a fair number of trigonometry functions and I was wondering how slow these functions are compared to more normal operations like addition, subtraction, multiplication and division (obviously they're slower but I was wondering how much and which is the slowest). In particular I was interested in square roots, sin, arcsin, cos, arccos. Also does anyone know of a link that would explain how the computer/programming languages calculates these functions? thanks, Mike

    P Offline
    P Offline
    Paul Conrad
    wrote on last edited by
    #3

    Not sure from the .NET stand point, but the trig instructions on the cpu can consume quite a few clock cycles ...

    "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

    1 Reply Last reply
    0
    • M MikeMarq

      I'm working on a project which needs to use a fair number of trigonometry functions and I was wondering how slow these functions are compared to more normal operations like addition, subtraction, multiplication and division (obviously they're slower but I was wondering how much and which is the slowest). In particular I was interested in square roots, sin, arcsin, cos, arccos. Also does anyone know of a link that would explain how the computer/programming languages calculates these functions? thanks, Mike

      C Offline
      C Offline
      cmk
      wrote on last edited by
      #4

      Although the CRT is free to wrap, or even ignore, the CPU float functions when calculating sqrt, sin, cos, ... the following should give a rough idea of relative costs. http://www.singlix.com/trdos/pentium.txt[^] So, on a pentium, the following asm instructions take the following # of clock cycles: FADD, FSUB, FMUL : 3 FDIV : 39 FSQRT : 70 FSIN, FCOS, FSINCOS, FPTAN : ~17-140 So, from the above we can make the following general recomendations: - If you need to divide several values by the same amount, instead multiple them by the reciprical amount. - sqrt is bad, try to avoid whenever possible. - If you need both sin and cos use sincos, you get one for free. - based on the low value for the trig functions it seems _likely_ (not sure) that they impliment a lookup table for some values, so adding your own on top will likely give little, if any, improvement.

      ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

      P M 2 Replies Last reply
      0
      • C cmk

        Although the CRT is free to wrap, or even ignore, the CPU float functions when calculating sqrt, sin, cos, ... the following should give a rough idea of relative costs. http://www.singlix.com/trdos/pentium.txt[^] So, on a pentium, the following asm instructions take the following # of clock cycles: FADD, FSUB, FMUL : 3 FDIV : 39 FSQRT : 70 FSIN, FCOS, FSINCOS, FPTAN : ~17-140 So, from the above we can make the following general recomendations: - If you need to divide several values by the same amount, instead multiple them by the reciprical amount. - sqrt is bad, try to avoid whenever possible. - If you need both sin and cos use sincos, you get one for free. - based on the low value for the trig functions it seems _likely_ (not sure) that they impliment a lookup table for some values, so adding your own on top will likely give little, if any, improvement.

        ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

        P Offline
        P Offline
        Paul Conrad
        wrote on last edited by
        #5

        cmk wrote:

        FSIN, FCOS, FSINCOS, FPTAN : ~17-140

        At least that wasn't the specs I saw when I first got my 80386 about 16 years ago (it was really hideous) :->

        "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

        D R 2 Replies Last reply
        0
        • C cmk

          Although the CRT is free to wrap, or even ignore, the CPU float functions when calculating sqrt, sin, cos, ... the following should give a rough idea of relative costs. http://www.singlix.com/trdos/pentium.txt[^] So, on a pentium, the following asm instructions take the following # of clock cycles: FADD, FSUB, FMUL : 3 FDIV : 39 FSQRT : 70 FSIN, FCOS, FSINCOS, FPTAN : ~17-140 So, from the above we can make the following general recomendations: - If you need to divide several values by the same amount, instead multiple them by the reciprical amount. - sqrt is bad, try to avoid whenever possible. - If you need both sin and cos use sincos, you get one for free. - based on the low value for the trig functions it seems _likely_ (not sure) that they impliment a lookup table for some values, so adding your own on top will likely give little, if any, improvement.

          ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

          M Offline
          M Offline
          MikeMarq
          wrote on last edited by
          #6

          Thanks everybody for the help. Wow it's suprising that division is that much slower than multiplication and sqrt is only double division. Interesting.

          C 1 Reply Last reply
          0
          • M MikeMarq

            Thanks everybody for the help. Wow it's suprising that division is that much slower than multiplication and sqrt is only double division. Interesting.

            C Offline
            C Offline
            cp9876
            wrote on last edited by
            #7

            Here are some interesting benchmarks of the Intel vector maths library - shows what can be achieved if the code can be vectorized; VML Performance[^]


            Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

            1 Reply Last reply
            0
            • P Paul Conrad

              cmk wrote:

              FSIN, FCOS, FSINCOS, FPTAN : ~17-140

              At least that wasn't the specs I saw when I first got my 80386 about 16 years ago (it was really hideous) :->

              "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #8

              remember the 386 had to do each flop in software as well because it didn't have an FPU, so you were taking a double hit there.

              -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

              P 1 Reply Last reply
              0
              • M MikeMarq

                I'm working on a project which needs to use a fair number of trigonometry functions and I was wondering how slow these functions are compared to more normal operations like addition, subtraction, multiplication and division (obviously they're slower but I was wondering how much and which is the slowest). In particular I was interested in square roots, sin, arcsin, cos, arccos. Also does anyone know of a link that would explain how the computer/programming languages calculates these functions? thanks, Mike

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #9

                Hi Mike, the other replies gave an impression of what you might be able to get, not of what you will actually get. The only way to know is to measure it! And what you get may well vary by CPU type, and application characteristics (float/double, integer/float balance, ...). :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                1 Reply Last reply
                0
                • D Dan Neely

                  remember the 386 had to do each flop in software as well because it didn't have an FPU, so you were taking a double hit there.

                  -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #10

                  My 386 had a math co-processor with it :-D Made a big difference.

                  "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                  D 1 Reply Last reply
                  0
                  • P Paul Conrad

                    My 386 had a math co-processor with it :-D Made a big difference.

                    "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                    D Offline
                    D Offline
                    Dan Neely
                    wrote on last edited by
                    #11

                    you only mentioned a 386 not a 387 :doh:

                    -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

                    P 1 Reply Last reply
                    0
                    • D Dan Neely

                      you only mentioned a 386 not a 387 :doh:

                      -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

                      P Offline
                      P Offline
                      Paul Conrad
                      wrote on last edited by
                      #12

                      My bad :-O I had a 386DX-25mhz to be exact. 4 megs of ram and an 80meg harddrive. Thought I was pretty slick back then :->

                      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                      1 Reply Last reply
                      0
                      • M MikeMarq

                        I'm working on a project which needs to use a fair number of trigonometry functions and I was wondering how slow these functions are compared to more normal operations like addition, subtraction, multiplication and division (obviously they're slower but I was wondering how much and which is the slowest). In particular I was interested in square roots, sin, arcsin, cos, arccos. Also does anyone know of a link that would explain how the computer/programming languages calculates these functions? thanks, Mike

                        A Offline
                        A Offline
                        Alan Balkany
                        wrote on last edited by
                        #13

                        I'm surprised no one mentioned this, but you can precompute the trig functions and build a lookup table. The lookup is faster than the computation. There's a tradeoff between the size of your table and precision. For values between two lookup table entries you can use a weighted sum to improve precision.

                        1 Reply Last reply
                        0
                        • M MikeMarq

                          I'm working on a project which needs to use a fair number of trigonometry functions and I was wondering how slow these functions are compared to more normal operations like addition, subtraction, multiplication and division (obviously they're slower but I was wondering how much and which is the slowest). In particular I was interested in square roots, sin, arcsin, cos, arccos. Also does anyone know of a link that would explain how the computer/programming languages calculates these functions? thanks, Mike

                          S Offline
                          S Offline
                          serge_bal
                          wrote on last edited by
                          #14

                          You can use approximate calculation instead. For example: sin(x) = x-(1/2)x^2+... This works for x near 0 !!!

                          1 Reply Last reply
                          0
                          • P Paul Conrad

                            cmk wrote:

                            FSIN, FCOS, FSINCOS, FPTAN : ~17-140

                            At least that wasn't the specs I saw when I first got my 80386 about 16 years ago (it was really hideous) :->

                            "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                            R Offline
                            R Offline
                            Robodroid
                            wrote on last edited by
                            #15

                            I wish I could find it right now...

                            For each computer I got (from 8088 up until a Pentium-Pro) I used to update a table where I showed the speed of all the trig functions along with mul and div. It was quite amusing to see how they sped up through the years. If I find it in the next couple of days, I'll post it up here....

                            'droid

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups