Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Jessy · Aug 21, 2010 at 05:27 PM · listmemorygenerics

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 &lt;int&gt; fingerIDs;
 public void Update ()
 {   
     if (pressState == PressState.Unpressed)
     {
         fingerIDs = new List&lt;int&gt;();
         for (int i = 0; i &lt; Input.touchCount; ++i)
         {
             Touch touch = Input.GetTouch(i);
             if (touch.phase == TouchPhase.Began &amp;&amp; rect.Contains(touch.position))
             {
                 pressState = PressState.Held;
                 fingerIDs.Add(touch.fingerId);
             }
         }
     }
     else if (pressState == PressState.Held)
     {
         List&lt;Touch&gt; touches = new List&lt;Touch&gt;();
         for (int i = 0; i &lt; 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 &lt; touches.Count; ++i) 
         {
             if (fingerIDs.Contains(touches[i].fingerId))
                 fingerIDs.Remove(touches[i].fingerId);
         }
         if (fingerIDs.Count &gt; 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 &lt; touches.Count; ++i)
             if (rect.Contains(touches[i].position)) fingerIDs.Add(touches[i].fingerId);
         if (fingerIDs.Count &gt; 0) pressState = PressState.Held;
         else if (pressState != PressState.Activated) pressState = PressState.Unpressed;
     }
     // After activation.
     else pressState = PressState.Unpressed;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Antony-Blackett · May 05, 2011 at 02:17 AM 0
Share

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

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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

How to add to a list excluding certain items 1 Answer

how to get a random integer in a list? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges