views:

93

answers:

2

Hey guys! Now I have one trouble for develope one iPhone app. I need to compute 150 parts and draw it on canvas. I have code like this:

    for (int i=x1; i<=x2; i++) {
            for (int j=y1; j<=y2; j++) {
                    x=[NSNumber numberWithInt:i];
                    y=[NSNumber numberWithInt:j];

                    BoxCache *box = [[cache objectForKey:x] objectForKey:y];

                    if (box) {
                            ret.count += [box.species count];
                    }

                    [x release];
                    [y release];
            }
    }

His execution tired ~5 secs (computing array 15x10), I would like to ask everyone, any ideas to reduce compute time or do that different.

+1  A: 

You'll probably need to give more information as your question is rather vague. Based on what you posted, however, I have two comments:

First, the question you asked. Why not use C arrays and ints, rather than (what appear to be) dictionaries? It's much faster.

Second, you're creating autoreleased NSNumber instances (+numberWithInt: gives you an autoreleased number), then over-releasing them with your calls to -release.

Things that need clarification for a better, more specific answer:

  1. What is BoxCache and how does it work?
  2. Where is the computation you mentioned in your first paragraph?
  3. What is "cache", where does it come from, and how does it work?
Joshua Nozzi
Not really sure about Objective C, but isn't it also the case that he is creating extra integer objects without needing to do so since he could just use the i and the j from the for loops?
X-Istence
NSDictionary requires objects as the keys. These are normally strings, but can be any object. Primitives are not allowed as keys.
Joshua Nozzi
A: 

Instead of a dictionary of dictionaries (which is really inefficient), I'd use a regular old C array and malloc the memory myself. I might wrap it in a class just to make the memory management rules clearer, but that's about it.

Dave DeLong