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
0
Question by d112570 · Nov 02, 2013 at 06:21 PM · findfor-loopforeach

Which is a faster find? (foreach) or (for-loop)

Which is faster using this code;

 for (int p = 1; p <=9; p++){
 GameObject.Find("Galaxies" + activeSystemM[0] + "/Stars/" + activeSystemM[1] + "/Planets/" + p + "/Ring/" + "Ring3");
 }

or

 Transform[] allChildren = GetComponentsInChildren<Transform>();
 foreach (Transform child in allChildren) {
 if (child.name == "Ring3") {
    var script3 = child.transform.gameObject.GetComponent<RingRotate>();
    if (!script3)  child.transform.gameObject.AddComponent("RingRotate")
 }
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
0
Best Answer

Answer by Freaking-Pingo · Nov 02, 2013 at 08:01 PM

First of all, you could have gone far with just a simple google search instead: C# For vs. Foreach

If you are wondering about performance, you should look into replacing your GameObject.Find() instead, and even your GetComponentsInChildren(); as these are much performance demanding than any for/foreach difference.

Comment
Add comment · Show 7 · 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 d112570 · Nov 02, 2013 at 08:09 PM 0
Share

Well my search was different then yours so I came up empty. so are you saying replace all the find for foreach?

avatar image Freaking-Pingo · Nov 02, 2013 at 08:39 PM 0
Share

I am not sure I understand your comment

avatar image d112570 · Nov 02, 2013 at 09:04 PM 0
Share

I mean, so you recommend using foreach since its faster then find().

avatar image Zaeran · Nov 02, 2013 at 09:15 PM 0
Share

It depends on unity's data structures and how methods like Find() and GetComponentsInChildren() traverse them.

If you really want to find out which is better, write both of them and time them, then use the faster one.

avatar image Dracorat · Nov 02, 2013 at 09:43 PM 1
Share

Premature optimization is the root of all evil.

Show more comments
avatar image
1

Answer by firemyst · May 20, 2016 at 06:21 AM

Which is faster depends on what you're iterating over. Here's a blog comparison that iterates over multiple kinds of objects, such as DataRows and custom objects, also including the performance of the While loop construct and not just the for and foreach constructs: http://cc.davelozinski.com/c-sharp/for-vs-foreach-vs-while

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

18 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

Related Questions

How to get values from nested JSON data with foreach or for loop? 1 Answer

newbie at C# needs help with for loop 2 Answers

Copy values between two classes in two lists. 1 Answer

Instantiate for loop skips some objects 1 Answer

Converting foreach touch into for. 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