1

I have a block of code that I am trying to optimize. The method is being used a lot so any little improvement would greatly increase performance.

- (CGRect)calculateRectForItemAtIndex:(NSIndexPath*)path {
    //Get the x position stored in an NSMutableArray
    double x = [self.sectionXPlacementArray[path.section] doubleValue];
    double y = 0;
    double height = 0;

    //If this is the first row it is a header so treat it different 
    if (path.row == 0) {
        height = self.defaultHeaderHeight;
        y = 0;
    }
    else {
        height = self.defaultHeight;
        //Calculate the Y placement 
        y = (path.row-1)*self.defaultHeight+self.defaultHeaderHeight;
    }
    //Build and return a CGRect
    return CGRectMake(x, y, [self.headerSizes[self.headers[path.section]] doubleValue],height);
}

Here is some more information:

1) headerSizes is a NSMutableDictionary that looks like so:

{
    Header1 = 135;
    Header2 = 130;
    Header3 = 130;
}

2) headers is a NSMutableArray that looks like this:

(
    Header1,
    Header2,
    Header3
)

These values in the app will not be Header_. They will be dynamic NSStrings like "City" or "State". headerSizes will contain the width that should be used for each header.

10
  • 10
    Are you sure that this method is actually slowing your app down? Commented Aug 23, 2013 at 13:40
  • 2
    no way this is going your bottleneck Commented Aug 23, 2013 at 13:43
  • Performance bottlenecks aren't apparent in your example. But, I am curious - this looks remarkably like a table. That being said, if performance is your concern, use a UITableView instead. Apple has all sorts of experience with this type of thing such as queueing cells and whatnot. Commented Aug 23, 2013 at 13:50
  • This is used in a very customized collectionView with unique placements and requirements. A UITableView would not would. Commented Aug 23, 2013 at 13:53
  • The way to demonstrate whether this is the problem is to simplify the code down to a constant return (just return the same value every time). It will never be faster than that, so if returning a constant doesn't improve your performance, then optimizing this code is unhelpful. Commented Aug 23, 2013 at 13:55

1 Answer 1

2

As another person commented, this does NOT look like a method that would be slowing anything down. It's involved in laying something out, right? Or drawing something? That should not be happening very often (i.e. 60 times per second at worst). Do you actually have any evidence that this is the bottleneck? Like, have you run your code through the Profiler template in Instruments? And this showed up as the #1 top method in an inverted-call-tree view of the data?

That said, there's not much to pare down here. I did my best...

- (CGRect)calculateRectForItemAtIndex:(NSIndexPath*)path
{
    //Get the x position stored in an NSMutableArray
    const NSUInteger pathSection = path.section;
    const NSUInteger pathRow = path.row;
    const float x = [self.sectionXPlacementArray[pathSection] floatValue];
    float y = 0;
    float height = 0;

    //If this is the first row it is a header so treat it different
    if (pathRow == 0) {
        height = self.defaultHeaderHeight;
        y = 0;
    }
    else {
        const float defaultHeight = self.defaultHeight;
        height = defaultHeight;
        //Calculate the Y placement
        y = (pathRow-1)*defaultHeight+self.defaultHeaderHeight;
    }
    //Build and return a CGRect
    return CGRectMake(x, y, [self.headerSizes[self.headers[pathSection]] floatValue], height);
}
Sign up to request clarification or add additional context in comments.

8 Comments

well, using floats instead of doubles should be an improvement with almost no precision lost.
I would expect that difference to be marginal, but then again, this whole question/answer is kinda bogus, 'cause there is no way that this method is the bottleneck, and if it is, then whatever's calling it is "doin' it wrong." (PS: I guess on ARM the penalty for double may be higher than x86_64. Don't feel like disassembling it and counting instructions and cycles. :) )
I definitely agree. My comment would be valid only on codereview.stackexchange :) It's just strange to use double where every type is defined as CGFloat.
Force of habit... I spend a lot of time in OSX where CGFloat is defined as double, and doing other stuff where FP precision matters (groan).
So why not declare them CGFloat and not have to worry which it is?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.