Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computational cost of trig functions [duplicate]

Tags:

Possible Duplicate:
How do Trigonometric functions work?

What actually goes into the computation of trig functions like Sin, Cos, Tan and Atan?

I think I've found an optimization in my code where I can avoid using any of these functions and base the problem around slope instead of angles. So that means a couple division operations in place of the above trig functions. But I'd like to know more about what goes into those trig functions so that I can compare my new code (from the perspective of number of basic math ops). Or maybe I've just found a more circuitous way of doing the same thing, or worse, introduced a less efficient method.

Using C++ and Python but I imagine these is fairly language agnostic with math operation cost being relative to the most primitive operations.

like image 465
ack Avatar asked Oct 01 '10 19:10

ack


2 Answers

You need to profile your code!

You need to profile this yourself. Based on my results, trig functions take about 100 ns and divisions about 20 ns. That can be easily converted to answers. But again, the most important thing is that you profile this on your hardware. That way you get exactly right answers and knowledge for your system.

like image 107
Peter Avatar answered Oct 02 '22 14:10

Peter


Modern x86 processors include trig functions in their instruction set, but they take many cycles to execute. So if you're on such a processor, and if you have no dependencies in your code (i.e. you don't need the result of one sin computation in order to start the next one), then you probably won't get much faster than using sin and cos directly, as they will be fully pipelined, achieving an effective rate of 1 per cycle.

like image 35
Oliver Charlesworth Avatar answered Oct 02 '22 12:10

Oliver Charlesworth