- Home /
Use new vector3 or a already defined vector3?
Hey,
So I was thinking about what is faster, I am developing a game for my android mobile so I want to make it as smooth as possible.
Is it faster to use a already defined vector3 and change its properties every frame:
private vector3 sampleVector;
Update(){
sampleVector = new vector3(1 * Time.time, 0, 0);
go1.transform.position = sampleVector;
}
or just use new vector3 every frame:
Update(){
go1.transform.position = new vector3(1 * Time.time, 0, 0);
}
I think the second one is better but one of my teachers mentioned something that the first one is better/faster because the memory is already saved.
Answer by tanoshimi · Sep 28, 2014 at 06:52 PM
A Vector3 is a struct, so defining a new
Vector3 requires very little overhead - memory gets allicated straight on the stack and doesn't involve the garbage collector at all.
If you really want to find out which is better, create a test that executes each version 100,000 times and profile the results, but I would be very surprised if the results are significant enough to consider worrying about.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163956im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Mesh Renderer and Memory 2 Answers
Unable to free resources from memory 0 Answers
how much memory is unity when installed? 2 Answers
Unity running out of memory 1 Answer