views:

45

answers:

1

For example, I try to do something like this:

- (BOOL)compare:(NSDecimal)leftOperand greaterThan:(NSDecimal)rightOperand {
    BOOL returnValue = NO;
    NSComparisonResult result = NSDecimalCompare(&leftOperand, &rightOperand);
    if (result == NSOrderedDescending) { // if the left operand is greater than the right operand
     returnValue = YES;
    }
    return returnValue;
}

But I wonder how big is the cost for memory when using this wrapper. The NSDecimalCompare function takes parameters by reference (is that the word?). But my method does not. I find that by-reference stuff hard to use. Does my method create copies of these values? Is it a waste of memory?

+3  A: 

You'll be making copies of your NSDecimals, but they're only 36-byte (if my math is correct) structs, so it might not be a significant overhead.

But is this really an issue? For example, are you calling this method many times per second? Sample your code first to see where the bottlenecks are before trying to optimize things like this. As Knuth says, "premature optimization is the root of all evil".

Shaggy Frog
mahboudz
WTH is Knuth? Please explain...
HelloMoon
Donald Knuth, http://en.wikipedia.org/wiki/Donald_Knuth
Shaggy Frog