- Home /
What is faster Rigidbody.velocity or assigned variable?
I use the velocity and position of an object about 20 times inside a function. In my case, it probably does not make a difference. Is using Rigidbody.velocity.x faster than assigning a float to it and using that? At what point if ever, would it be faster to assign a variable?
So if this were my code:
Rigidbody2D rb = gameObject.GetComponent<Rigidbody2D>();
float vx = rb.velocity.x;
Would it be better to include line 2 and use the variable or just use rb.velocity.x?
Answer by Eno-Khaon · Feb 11 at 07:55 PM
At a fundamental level, if you want to compare performance between values types and usage, just try it!
For example:
void VariableAccessSpeedTest()
{
float assignedValue;
float readValue;
float startTime;
float endTime;
// Assign value 100k times
// Source: Rigidbody->Vector3->float
startTime = Time.realTimeSinceStartup;
for(int i = 0; i < 100000; i++)
{
assignedValue = rb.velocity.x;
}
endTime = Time.realTimeSinceStartup;
Debug.Log(string.Format("100k assignments (Rigidbody->Vector3->float): {0} seconds", endTime - startTime));
// Assign value 100k times
// Source: float
startTime = Time.realTimeSinceStartup;
readValue = rb.velocity.x;
for(int i = 0; i < 100000; i++)
{
assignedValue = readValue;
}
endTime = Time.realTimeSinceStartup;
Debug.Log(string.Format("100k assignments (float): {0} seconds", endTime - startTime));
}
Barring the most extreme circumstances (i.e. a HUGE number of uses at a time or actual GetComponent/Find calls or similar), the differences in variable access speed should be so negligible, however, that creating and assigning the new float value could very well wind up being *SLOWER* than using them in place a few times.
That doesn't mean you won't see a potential speed advantage in using "temporary" variables, however. It's important to note, however, that completely local variables will be much slower than ones that are not. For example:
// Declared outside any region where it's used
// Declared once
float tempVariableFastest;
void Update()
{
// Declared outside the region where it's used most
// But still declared every pass through the function
float tempVariableFast;
for(int i = 0; i < 100000; i++)
{
// Declared immediately before use
// Redeclared on every pass through the loop
float tempVariableSlow;
}
}
Why do you think that a local variable would be slower than a member variable of a class? Local variables live on the call stack and unless you're doing some fancy closure stuff with it which requires an allocation on the heap, local variables are usually faster than any member variable access. Member variables have to be accessed through an object pointer (which usually lives on the stack as well) plus an offset inside the class while local variables are just an offset from the base pointer of the current stack segment.
For example here are my TextureTools which provide two methods to resample and crop / letterbox a texture to a new target size which I made for this question. Here's the IL code for the crop method with the original C# code as comments. As you can see all local variables are declared up front on the stack and only referred to by their offset on the stack. Of course this is just intermediate language code so when jitted it may be even simplifed more. Though it won't get more complicated.
.
As I said when you have a closure around a variable inside a loop, this is where you would run into troubles because the compiler has to allocate a new closure object on the heap each iteration. Though technically the variable is no longer local in that case.
ps: note that my TextureTools were used on images that are 5k x 5k, so about 25 million pixels and I have tons of local variables inside the loop and they are not really an issue. Reducing method calls can speed up things quite a bit, especially when you have a tight loop.
Well, I won't deny that I may have mixed up C++ and C# in this situation. I've been going back and forth between them a lot in my last project and specifically found a significant difference (30+% time difference in my tests) in performance depending on use/reuse of variables in the context I described (definitely in C++, but perhaps overlooked for C#).
Granted, that's also obviously dependent on compiler behavior for either language, but that's also kind've the point, I suppose...
int exampleA;
// ...
for(int i = 0; i < 1000; i++)
{
exampleA = 1;
}
// vs...
for(int i = 0; i < 1000; i++)
{
int exampleB = 1;
}
In the context in which I recently used it (again, this might've been C++ in this case, since I was aiming to maximize performance in a few tests more than a C# rough-equivalent I was making alongside it), exampleB was significantly slower in processing its loop than exampleA. There's a bit more to the loop(s) than just that in practice, but that was quite literally the only change I made at the time (with otherwise matching conditions and a few dozen tests each), so it probably *DOES* boil down to a matter of resulting compilation. So, all things considered, I probably am conflating the two languages in this situation.
I looks like you misunderstood my question. In your example you are not using the member variable. In both instances you assigned the member variable to a local variable and just used the local variable.
Your answer