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 headkitgames · Mar 24, 2011 at 10:07 AM · iphonefastindexof

How fast is "System.Array.IndexOf" on iPhone?

hi there!

I was wondering what would be faster, using for(...) or foreach(...) on arrays in C# on iPhone like

foreach (GameObject foo in bArray) int index = System.Array.IndexOf(bArray, foo);

or

for(int i=0; i<= bArray.length-1; i++) int index = i;

thnx!

Comment
Add comment · Show 2
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 Eric5h5 · Mar 24, 2011 at 10:17 AM 2
Share

What does making a test, running it on the device, and using the profiler tell you?

avatar image Statement · Mar 24, 2011 at 10:47 AM 1
Share

Your for loop looks a little funny though. No need to decrement length. for(int i=0; i < bArray.length; i++)

2 Replies

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

Answer by Statement · Mar 24, 2011 at 11:10 AM

A for loop is faster and should be used when you want to make use of the index. Any index searching functions just add overhead.

for(int i=0; i < bArray.length; i++) 
    int index = i;

IndexOf need to walk through the array so your time complexity would be squared. So for large lists it will become slower (Well I'd be surprised if the foreach ever would be faster)

foreach (GameObject foo in bArray) int index = System.Array.IndexOf(bArray, foo); // <- for/each inside

// basically the same as:

foreach (GameObject foo in bArray) { int index = -1; for (int i = 0; i < bArray.length; ++i) { if (bArray[i] == foo) { index = i; break; } } }

So you can imagine it is quite slower.

Comment
Add comment · Show 4 · 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 Mike 3 · Mar 24, 2011 at 11:41 AM 1
Share

Small note about "A for loop is faster" - it depends on circumstance. With a native array, the CIL created is the same for both foreach and for, meaning identical speed.

avatar image Statement · Mar 24, 2011 at 11:45 AM 1
Share

While it's true that compilers can make use of some quick cheats for stuff like arrays, a foreach works on an IEnumerator, meaning callbacks for testing and enumerating the set. I havnen't personally checked out the generated IL, I'll take your word for it. But foreach on other classes will cause a slight overhead. The overhead is so small it often have no practical value, but still it is overhead.

avatar image Bunny83 · Mar 24, 2011 at 02:22 PM 0
Share

I often use foreach loops if i just need to iterate through the elements, but when I need the corresponding index I'll always use a "normal" for(int i ...) loop. IndexOf on native arrays is quite slow depending on the element count. IndexOf will iterate through the array until it finds the element. In search optimised classes like Dictionary it's quite fast but still an overhead.

avatar image headkitgames · Apr 05, 2011 at 04:26 PM 0
Share

t h a n k y o u !

avatar image
2

Answer by Mike 3 · Mar 24, 2011 at 10:21 AM

Your first example makes little sense, so the second one

IndexOf should be relatively fast, but it's not for getting the count in a loop - use a count variable for that and increment it each time

Regarding for vs foreach - it depends.

Usually you'll want to use whichever is easiest to read, there's very little (if any) difference in speed for native arrays

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

Code differences between Unity Editor and real iPhone ; a String.Split or IndexOf problem. 2 Answers

Optimize my game? 2 Answers

does ios require physical phone 1 Answer

Zero-ing the Input.acceleration.z 0 Answers

Unity modules for iOS? 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