I'm creating and translating a few algorithms when I'm wondering which is faster?
a) (int)float
or
b) Mathf.FloorToInt(float)
Thanks in advance.
EDIT: If there is a faster way than either of those, that would be helpful too.
I'm creating and translating a few algorithms when I'm wondering which is faster?
a) (int)float
or
b) Mathf.FloorToInt(float)
Thanks in advance.
EDIT: If there is a faster way than either of those, that would be helpful too.
Do a test with Stopwatch like I mentioned. This answer is here because I believe that the result in your answer is wrong.
Below is a simple performance test script that uses loop since your algorithm involves many loops:
void Start()
{
int iterations = 10000000;
//TEST 1
Stopwatch stopwatch1 = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
int test1 = (int)0.5f;
}
stopwatch1.Stop();
//TEST 2
Stopwatch stopwatch2 = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
int test2 = Mathf.FloorToInt(0.5f);
}
stopwatch2.Stop();
//SHOW RESULT
WriteLog(String.Format("(int)float: {0}", stopwatch1.ElapsedMilliseconds));
WriteLog(String.Format("Mathf.FloorToInt: {0}", stopwatch2.ElapsedMilliseconds));
}
void WriteLog(string log)
{
UnityEngine.Debug.Log(log);
}
Output:
(int)float: 73
Mathf.FloorToInt: 521
(int)float
is clearly faster than Mathf.FloorToInt
. For stuff like this, it is really bad to use the FPS from the Editor Stats to make the judgement. You do a test with the Stopwatch
. The FPS should be used when writing shader code.
It depends on your target platform.
In the Editor, where you're running .NET code that hasn't been optimized, (int)
is much faster than Mathf.Floor()
because the latter includes the extra expense of function calls.
However, when you compile a build, your code may be optimized differently depending on your target platform and build settings. Many build targets use IL2CPP to transpile your code. With IL2CPP, your C# code is compiled to .NET intermediate language (IL), which is then translated to C++ (CPP), which is then compiled into a binary for your target platform (or compiled to WebAssembly for WebGL builds).
The compilers are not stupid. They will optimize or even inline function calls where practical. In other words, the compiler may end up replacing Mathf.Floor()
with something that works very similarly to (int)
.
For example, in my own testing, I've found that (int)
can be 20x faster than Mathf.Floor()
in the Editor, but in a WebGL build there's no difference in speed.
Never make decisions about code performance based only on testing in the Editor. You should always test performance-critical code on your target platform. That said, it's rare for your game code to bottleneck on this kind of thing. Usually performance bottlenecks come from graphics, algorithms, or abusing Unity APIs (e.g. constantly calling GameObject.Find()).