In this thread we were talking the other day about the performance of sin()
calculations vs. using curves so I made a quick & dirty benchmark to compare them.
Simple calculations
Lets call these very simple calculations 1000 times.
Sine
var h := sin(0.5)
var v := sin(0.5)
Simple curve
var h := simple_curve.sample(0.2)
var v := simple_curve.sample(0.2)
Results
sin()
: 76 μs
Curves: 111 μs
More complex calculation
This is a more real file example. Actually at its core it's the same calculations I made in the other thread.
Sine
var time := Time.get_unix_time_from_system()
for i in iterations:
var t_hori := wrapf(time, 0.0, 1.0)
var t_vert := wrapf(time + 0.25, 0.0, 1.0) * 2.0
var r := t_vert * PI
var v := Vector2(sin(t_hori * 2.0 * PI), 1.0 - sin(r if r < PI else r - PI))
Curves
For the curves I am using these ones from the other thread:
var time := Time.get_unix_time_from_system()
for i in iterations:
var t := wrapf(time, 0.0, 1.0)
var v := Vector2(horizontal_sway.sample(t), vertical_sway.sample(t))
Results
sin()
: 294 μs
Curves: 120 μs
Conclusion
As long as the calculations are simple calling something like sin()
is faster but the moment it gets more complicated and you need to write more code then using curves is faster because of the GDScript overhead.