- Home /
Is "new List<T>()" worse than using Clear()?
For example, you can see that I'm using new List<> to reset touches in this code. I could use Clear instead, but then I'd have to have touches previously constructed, which is clutter. However, I'll do that kind of thing if it is better practice for memory allocation.
For fingerIDs, I don't even need to reconstruct the list every time, or even clear it. Again, I just put it where I did to not have to construct it earlier. I assume your answer to my question will answer the other question of how bad it is to do this, but feel free to specifically comment on that, and if you think it's worth checking to see if it's null before reconstructing it.
using UnityEngine; using System.Collections.Generic;
public enum PressState { Unpressed, Held, Activated }
public class Button { public PressState pressState; public Rect rect;
List <int> fingerIDs;
public void Update ()
{
if (pressState == PressState.Unpressed)
{
fingerIDs = new List<int>();
for (int i = 0; i < Input.touchCount; ++i)
{
Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began && rect.Contains(touch.position))
{
pressState = PressState.Held;
fingerIDs.Add(touch.fingerId);
}
}
}
else if (pressState == PressState.Held)
{
List<Touch> touches = new List<Touch>();
for (int i = 0; i < Input.touchCount; ++i)
touches.Add(Input.GetTouch(i));
// Activate if holding finger(s) left the screen.
// Can be overridden in the next loop,
// if other fingers are touching the button.
// It won't be activated if some fingers left and
// others just slid elsewhere.
for (int i = 0; i < touches.Count; ++i)
{
if (fingerIDs.Contains(touches[i].fingerId))
fingerIDs.Remove(touches[i].fingerId);
}
if (fingerIDs.Count > 0)
{
pressState = PressState.Activated;
fingerIDs.Clear();
}
// After it's pressed, allow any other fingers to keep it held,
// no matter what they were doing before touching the button.
for (int i = 0; i < touches.Count; ++i)
if (rect.Contains(touches[i].position)) fingerIDs.Add(touches[i].fingerId);
if (fingerIDs.Count > 0) pressState = PressState.Held;
else if (pressState != PressState.Activated) pressState = PressState.Unpressed;
}
// After activation.
else pressState = PressState.Unpressed;
}
}
Answer by Lucas Meijer 1 · Sep 22, 2010 at 11:54 AM
Clear() is very likely to perform better. On mobile devices it's especially important to try to limit memory allocations to a minimum in the Update() method.
I suspect the List implementation internally keeps an T[] array around, which it can recycle when you call Clear(). when you add "too much stuff", it will likely throw away its array, and create a new one double the size.
You can stop further fragmentation of memory by using the capacity parameter on new List( int capacity ). This will allocate a block to hold the elements and require no resizing until capacity is exceeded. http://msdn.microsoft.com/en-us/library/y52x03h2.aspx
Answer by Peter G · Aug 21, 2010 at 08:16 PM
I don't have any stats, but this SO page has a similar question but it is related to System.Collections. I don't know how much if any this answer would be different if this were a List<>() instead of a Collections such as an ArrayList, but maybe this will get you started.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071758im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to make sense of error message? 1 Answer
How to get smallest element and key from int list 1 Answer