animation curve vs mathf.sin

I am wondering about using animation curve vs using mathf.sin or cos for evaluating sin or cos like graph.

Which is faster in terms of performance if you disregard curve’s advantage of being able to freely make graph?

So for example :

draw sin like graph using animation curve and evaluate value at time

vs

use mathf.sin function to calculate sin value at time

Anyone?

I ran a test with 1mil iterations and on my machine the animationCurve with three keyframes is 18.7% faster than cos.

public class CosVSCurveTest : MonoBehaviour
{
	AnimationCurve curve;
	
	const int ITERATION_COUNT = 1000000;
	
	
	void Start()
	{
		Profiler.enabled = true;
		
		curve = new AnimationCurve();
		curve.AddKey( new Keyframe( 0, 1, 0, 0 ) );
		curve.AddKey( new Keyframe( 0.5f, -1, 0, 0 ) );
		curve.AddKey( new Keyframe( 1, 1, 0, 0 ) );
	}
	
	
	void Update()
	{
		float normalizer = 1 / (float) ITERATION_COUNT;
		
		Profiler.BeginSample( "HALLOCos" );
		for( int i = 0; i < ITERATION_COUNT; i++ ){
			float normal = i * normalizer;
			float value = Mathf.Cos( normal );
		}
		Profiler.EndSample();
		
		Profiler.BeginSample( "HALLOCurve" );
		for( int i = 0; i < ITERATION_COUNT; i++ ){
			float normal = i * normalizer;
			float value = curve.Evaluate( normal );
		}
		Profiler.EndSample();
	}
}

18.7% !

That is good enough reason for me to not use Maths.sin

Thanks for the test…

I was guessing that it would be the case, but it still surprise me a bit. I guess there are cases where sin makes more sense even if it is slower but this is very useful information indeed. Cheers.