A question about picking efficiently

0 favourites
  • 4 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • Hey there, I have a short question about picking instances.

    Is picking instances by UID/IID more efficient than by comparing an instance variable?

    To my understanding picking by comparing an instance variable scans all existing instances of an object to find matches. UID/IID in contrast are unique, so does this mean Construct immediately picks the correct one without having to scan instances until it finds the matching UID/IID?

    Edit: This is purely theoretical, I understand that performancewise this won't matter 99,9% of the time. I'd simply like to know what's happening under the hood!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This is easy to test:

    On my PC picking the sprite 10 million times by IID is the fastest - takes 8 seconds, by UID takes 10 seconds, and by instance variable takes 13 seconds.

  • Picking by instance variable is like a filter.

    It loops over all the picked instances to find matching to pick.

    It’s O(n) which means time it takes is proportionate to the number of instances

    Pick nth instance should be more or less instant. It’s just an array lookup.

    Pick by uid is like a dictionary lookup. Should also be basically instant. Js impliments dictionaries very efficiently.

    Both of those are O(1) which means they take the same amount of time regardless of the number of instances.

  • dop2000 Thank you for taking the time to test it! I tried to test it myself but my solution for testing was really stupid compared to yours and didn't yield usable results.

    R0J0hound Thank you, for explaining this in detail! That's exactly what I was looking for but couldn't find anywhere.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)