Effective 15x10 array computation
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 r开发者_运维知识库elease];
[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.
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.
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:
- What is BoxCache and how does it work?
- Where is the computation you mentioned in your first paragraph?
- What is "cache", where does it come from, and how does it work?
精彩评论