Hello everyone!
I’ve recently been working on a large open world space project and I (obviously) had to tackle the issue of float precision. This lead me to think of the performance difference between available number types so I made a few tests that may interest some of you.
For each operation I made a function that applies a few operations to a variable of different types. I repeated this functions quite a lot of time and calculated the time to execute the total loop using Time.realtimesincestartup. The results are given in nanoseconds, approximately represent the time taken to make one operation (like one addition or one sqrt) AND SHOULDN’T be used to compare the speed difference between different operations(they’re probably about the right magnitude order, nothing more), only between different var types. For each type I ran the code ~400 million times and I checked the consistency by running all of that a few times. I used a laptop with a 3500u at ~3GHz.
Here are the results I got:
(Thanks Peter77 for the table)This isn’t an accurate and reliable comparison, but I guess it can offer a good enough estimation.
To resume the results, from best to worse:
Int<Double<Short~Long~Float
This seems quite surprising (especially double beeing faster than float) and if someone with a deeper understanding of computer science than me could explain that, it would be interesting.
Edit: here’s a resume of what’s been said below:
Basically, this test isn’t really useful, wether it be because of everything required to do a meaningful test or the fact that this timings are so small that the difference doesn’t matter except maybe for the most extreme cases (in wich case the actual benefit of using different types will depend on your situatuion). Also sorry for not posting the source code, I’ll definitevly do that next time ![]()
