• 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 /
This question was closed Aug 19, 2017 at 07:40 AM by GnomenxD for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by GnomenxD · Aug 15, 2017 at 11:57 PM · listperformance optimizationfindobjectsoftype

FindObjectsOfType vs List.Contains Performance?

Hello.

I have a very quick question I was wondering which one would have least spike in the performance. I have an object which has to locate another object depending on a specific ID. Here I uses FindObjectsOfType every time, it's a one time event called a few times each minute. Then I thought I could create a list and when it finds objects of that type throw them into the list and later use list.Contain() and if list doesn't contain the object use FindObjectsOfType again and throw the new objects into the list. Would this improve performance or wouldn't it actually matter?

Kind regards.

Comment
Add comment · Show 1
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 Ali-hatem · Aug 16, 2017 at 11:33 AM 0
Share

the List of curse and if you can completely avoid the FindObjectsOfType this even much better.FindObjectsOfType is a very slow function because it has to iterate throw the whole objects in the scene each time you call it.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by ShadyProductions · Aug 16, 2017 at 07:50 PM

There is something exactly for this, a dictionary. It can use the TryGetValue call which is a very fast call to retrieve / cache already found objects by a unique key. Basically you give a key and a value to a dictionary, where you can then retrieve the value by its key. Here's an example of pseudo code where I use an int as key and gameobject as value:

 Dictionary<int, GameObject> cachedObjects = new Dictionary<int, GameObject>();
 
 GameObject connectedObject; //our holder for when we do find the object
 // Locate the other gameobject by specific id
 if (cachedObjects.TryGetValue(id, out connectedObject)) {
 // If we already have some object by this specific ID then do w/e
 // our gameobject will be inside the connectedObject variable
 }
 else 
 {
 // Do w/e expensive call to retrieve the object you require
 //add the retrieved object by the specific id as key so we can get it fast nexttime
 //without having to do the expensive call to retrieve it again
 cachedObjects.Add(id, retrievedgameobject); 
 }

Do note a dictionary can only have UNIQUE keys. If one key can store more objects then your value will have to be a list of gameobjects like so:

 Dictionary<int, List<GameObject>> cachedObjects = new Dictionary<int, List<GameObject>>();

 List<GameObject> connectedObjects;
 if (cachedObjects.TryGetValue(id, out connectedObjects)) {
     // we already have a list of objects so we can just add to it or use them
     connectObjects.Add(someOtherObject);
 }
 else 
 {
 // we don't have a list yet
 var listdata = new List<GameObject>();
 listdata.Add(someitem);
 // do some expensive calls and add items to the list, then add the list with key to dictionary
 cachedObjects.Add(id, listdata); 
 }
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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to improve the Performance of Removing Trees during runtime? 1 Answer

List Performance Question 2 Answers

How to get the component from all the gameobject in list? 1 Answer

Multiple Nested lists 0 Answers


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