Best Practices: How about floats and double?

I have a music system that supports loops, so for that system, I need double precision - float just doesn’t cut it. That system also mostly lives in the MonoBehaviour-world (because it’s comparatively trivial in terms of number of objects and complexity of calculations).

This system interacts with a DOTS world that needs time as well as loop points. For the DOTS world, float precision is just fine, that’s precise enough because it’s just visuals and we don’t ever go beyond 144 FPS.

So the question: double being less efficient, and comparing floats and doubles probably being even worse in terms of performance, would you recommend:

a) having different datastructures for time-related properties between DOTS and MonoBehaviour, with the conversion only happening going from MonoBehaviour to DOTS, once per frame? With just a handful of variables that this is relevant for, I’m even considering keeping both (double + float, with a XPrecise naming convention).

b) not over-optimize and just use double because comparing double and float isn’t that bad, even when dealing with tens of thousands of comparisons per frame?

I’d recommend profiling and seeing if it is a performance bottleneck before trying to optimize it. Doubles aren’t as bad as people make them out to be, especially if the code was going to be scalar anyways and there’s enough computation to not be memory-bound (which is more likely when dealing with scalar code). I recently saw a pretty heavy pathfinding algorithm perform identically with 32 bit and 64 bit on ARMv7. It was only when I manually vectorized the code with intrinsics (because the compiler I was using gave up trying to vectorize any comparison operation for neon) that floats were faster.

1 Like

As I know in “normal” c# environment (Windows etc. ).
There should be no difference when it comes to mathematical operations between float and double.
The performance drop for double is usually associated with memory write/read.
I don’t know about casting float ↔ double performance.

1 Like