Author Topic: Most efficient search method?  (Read 2771 times)

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Most efficient search method?
« on: June 05, 2012, 06:18:01 AM »
This is probably more of a Unity question than an NGUI question, however I am curious as to how the objects are stored in NGUI and how best to traverse the child list if I am planning on doing an algorithm every frame.

Let's say I have these 2 algorithms:-

____________1___________

for (int i = 0; i < transform.GetChildCount(); i++)
{
  transform.GetChild(i).renderer.material.SetColor("_Color", new Color(1.0f, 0, 0, 1.0f));
}
_______________________


____________2___________

Transform[] transform_list = myobj.GetComponentsInChildren<Transform>();

foreach (Transform the_t in transform_list)
{
    the_t.renderer.material.SetColor("_Color", new Color(1.0f, 0, 0, 1.0f));
}

_______________________


Now off the top of my head I am thinking that algorithm 2 is faster because in 1, the GetChild is being called every frame (and perhaps is also being evaluated each time in the for loop as well). However algorithm 2 requires an allocation of memory for the list, so hmm, not sure which is best.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Most efficient search method?
« Reply #1 on: June 05, 2012, 06:44:32 AM »
As far as I know, either one is sorta slow and you don't want to be doing them each update. What I would recommend is to do the proper traverse once and cache everything in a builtin array
  1. Transform[] myChildren; //yadda yadda

and run through that instead on later runs.

Obviously if your children change then it won't work unless you also update the array.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Most efficient search method?
« Reply #2 on: June 05, 2012, 06:52:48 AM »
A proper traverse array? How do I go about doing that? Do you mean as in using a hash table/dictionary list?

dlewis

  • Guest
Re: Most efficient search method?
« Reply #3 on: June 05, 2012, 07:34:55 AM »
A proper traverse array? How do I go about doing that? Do you mean as in using a hash table/dictionary list?

The way I would do it would be to make the size of the array equal to the number of children that there are in the object (sudo code follows).

Transform[] children;
int numChildren = go.GetNumChildren(); // You'll probably have to look this one up. If I remember it's not a function in the documentation but you can get it some other way
children = new Transform[numChildren];

foreach (Transform trans in children)
{
// If foreach doesn't work use for (;;). Coming from C++ I would use the for loop but that's just me!
}

rain

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 79
    • View Profile
Re: Most efficient search method?
« Reply #4 on: June 05, 2012, 01:40:39 PM »
There is a function called getchildcount or whatsoever which gives the number of children in a given object, but i prefer storing the components in an array and iterating through them using foreach like "ENAY"'s 2. algorithm. It's faster that way since you have direct access to them.