The number of lines of code is not necessarily the only optimization you should consider. Trigonometric functions are expensive in terms of the time it takes to complete one calculation (i.e., a single call to cos () may require hundreds of additions and multiplications depending on the implementation).
In the case of the widely used signal processing function, discrete Fourier transform, the results of thousands of calculations cos () and sin () are pre-computed and stored in a massive search table. The trade-off is that you use more memory when you run your application, but it works much faster.
See the following article or search for the importance of “pre-calculated twisting factors,” which essentially means computing tons of complex exponentials well in advance.
In the future, you should also indicate what you are trying to optimize (i.e. used processor cycles, number of bytes of used memory, cost, among other things). I can only assume that you want to optimize in terms of the instructions being executed and, in addition, the number of processor cycles used (i.e.: you want to reduce CPU overhead).
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.34.9421&rep=rep1&type=pdf
source share