###
Trig performance test

I have asserted that trigonometry is slow in a number of posts. The whole point of a post on Trig-free rotation blending is that trigonometry is so slow that it should be avoided. My reasons for this are my experiences on previous platforms, where trigonometric functions are often tens or hundreds of times slower than arithmetic operations.

But such assumptions often prove false on new platforms. Flash in particular does not have the highly optimised arithmetic operations of modern CPUs, so rather than trigonometry being especially slow it could be that maths in Flash is slow enough that there's no need to avoid Flash. This is certainly worth knowing, as there's no point avoiding particular functions if they are as fast as other ways of doing the same thing.
So I wrote a simple test, available here, to measure the benefit of using pre-calculated values instead of trigonometric functions. The test simulates getting the sin and cos of an angle which is one of 256 possible angles, to simulate something which can only point in a limited number of directions. This arises often in games where things (such as an aiming turret) rotate at a fixed speed with a fixed rotation rate. If the speed is constrained so it travels a fraction of a turn a frame (e.g. 1/256) then all the angles and the sin and cos of them can be pre-calculated.

The results I get with the test app are that the trigonometric functions are three times slower than using pre-calculated values. This is significant but nothing like the performance difference seen in C/C++ code. The reason is the overhead of Flash in general, in particular the Vector access which uses an expensive function, but also the arithmetic and loop which are all much slower than in a compiled language.

The other point is the greatest benefits are seen if angles and trigonometry are avoided altogether, as covered in a number of previous posts. In many games although there are directions for drawing, aiming and motion there's often no need to use angles, so calculations can be done without converting to and from them using trigonometry, potentially speeding calculations by much more than a factor of three.

Trigonometry is used throughout mathematics, and, since mathematics is applied throughout the natural and social sciences, trigonometry has many applications. Calculus, linear algebra, and statistics, in particular, use trigonometry and have many applications in the all the sciences.

ReplyDeletetypes of angles