### 问题

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.

### 回答1:

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.

### 回答2:

# 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.

### 回答3:

(This was originally a comment on codekaizen's answer, but It got rather long....)

(Codekaizen): Most trig functions are implemented as lookup tables these days.

um.. Since most trig function take a double precision argument, looking up the value isn't practical. I believe most look up the integers on either side and then interpolate from there (i.e. Sin(5.279) is 27.9% the way from Sin(5) to Sin(6)). That less work that calculating the value directly, but still a fair amount of calculations.

### 回答4:

Most trig functions are implemented as lookup tables these days.

### 回答5:

The only real answer you're going to get it *"Profile."*

Most likely, if this is not a bottleneck in your code it will make no noticeable difference whatsoever.

### 回答6:

My experience from trigonometric functions are that they are extremely fast, and that most of them are **implemented as lookup tables anyway**... That is, a few divisions and division-by-zero checks will probably be slower than a call to a trigonometric function.

### 回答7:

Take a look at glibc. It uses several different implementations some of them (like sysdeps/ieee754/s_sin.c) look really complicated while others use an assembly instruction (like sysdeps/x86_64/fpu/s_sincos.S). It is hard to tell the actual time required without some measurements.

### 回答8:

The near optimal method (and the method typically used) for evaluating trigonometric functions is through an orthogonal polynomial expansion (Chebyshev series.) Such a series with an appropriate number of terms will be faster than table lookup.

http://en.wikipedia.org/wiki/Chebyshev_polynomials

来源：`https://stackoverflow.com/questions/3842246/computational-cost-of-trig-functions`