I am profiling an iPhone app and I noticed a strange pattern. In a certain block of code that’s called quite frequently…
[item setQuadrant:[NSNumber numberWithInt:a]];
[item setIndex:[NSNumber numberWithInt:b]];
[item setTimestamp:[NSNumber numberWithInt:c]];
[item setState:[NSNumber numberWithInt:d]];
[item setCompletionPercentage:[NSNumber numberWithInt:e]];
[item setId_:[NSNumber numberWithInt:f]];
…the first call to [NSNumber numberWithInt:] takes an inordinate amount of time, in the order of 10-15x that of the remaining calls. I’ve verified that the results are consistent if I shuffle the lines (the first line is always the slow one, by the same ratio). Is there something going on that I’m not aware of?
Perhaps this happens because this block is inside a try/catch?
If I had to guess, NSNumber performs some code in it’s
+loadimplementation, which slows down the initial call to the class. Also note thatNSNumbercaches it’s return value, so future calls to+numberWithInt:with the same value are faster than before, that could possibly be part of the issue.