Relative Speed of Trigonometry functions
-
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
-
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
MikeMarq wrote:
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
The first order response is: they are all equally slow, anywhere between 10 and 100 times slower than multiplication. If it is really important to you, perform some simple experiments to see how they behave on YOUR system with YOUR software environment. some Google result[^]. :)
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
-
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
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
-
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
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
-
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
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
-
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
-
Thanks everybody for the help. Wow it's suprising that division is that much slower than multiplication and sqrt is only double division. Interesting.
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."
-
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
-
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
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
-
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.
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
-
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
-
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.
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
-
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
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.
-
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
-
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
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