Tuesday, March 19, 2013

How slow is CGRectMake? Or: Paul times stupid things so you don't have to!

I just love the CGGeometry stuff. Apple seems to think using them is a good idea. Other people don't like them as much. I feel like developers who really want to be programming iOS or MacOS embrace them. Developers who would really rather be programming something else avoid them. That's just my opinion about it.

OK, having said that, how slow is CGRectMake?

In case you have never looked, here is what CGRectMake actually does:


CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
  CGRect rect;
  rect.origin.x = x; rect.origin.y = y;
  rect.size.width = width; rect.size.height = height;
  return rect;
}

Now I wonder why they didn't just: return (CGRect){{x,y},{width,height}}; Does anyone know? Also, I love that Apple puts that opening { on its own line. That was my pattern until I was beaten into submission at the NYTimes by Matt and Brittany.


Here is my test code:


CGFloat x = 1.0;
CGFloat y = 1.0;
CGFloat width = 1.0;
CGFloat height = 1.0;

NSDate *startTime = [NSDate date];
for (NSInteger loop = 0; loop < 1000000000; loop++) {
    CGRect rect = CGRectMake(x, y, width, height);
}
NSLog(@"elapsed time of CGRectMake: %f", -[startTime timeIntervalSinceNow]);
    
startTime = [NSDate date];
for (NSInteger loop = 0; loop < 1000000000; loop++) {
    CGRect rect = {{x,y},{width,height}};
}
NSLog(@"elapsed time of bracket rect make: %f", -[startTime timeIntervalSinceNow]);

Here are the results:

2013-03-19 22:46:02.391 CGRectMakeTest[38559:c07] elapsed time of CGRectMake: 14.657779
2013-03-19 22:46:05.212 CGRectMakeTest[38559:c07] elapsed time of bracket rect make: 2.818589

The results show that CGRectMake takes 5.2 times as long as creating the struct using brackets. Wow, that's a long time! But it only took 0.00000001465778 seconds per call (admittedly on the iPhone Simulator on my MacBook Air. I was just too lazy to go get my cable to plug in the phone).

I'll keep using CGRectMake, thanks.

Postscript: I made my own version of CGRectMake to test the time required to just return the bracket rect, like so:


CG_INLINE CGRect
MYCGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
    return (CGRect){{x,y},{width,height}};
}

startTime = [NSDate date];
for (NSInteger loop = 0; loop < 1000000000; loop++) {
    CGRect rect = MYCGRectMake(x, y, width, height);
}
NSLog(@"elapsed time of MYCGRectmake: %f", -[startTime timeIntervalSinceNow]);


Here are those results:


2013-03-19 23:28:39.452 CGRectMakeTest[44092:c07] elapsed time of CGRectMake: 14.612408
2013-03-19 23:28:42.331 CGRectMakeTest[44092:c07] elapsed time of bracket rect make: 2.877312
2013-03-19 23:28:56.775 CGRectMakeTest[44092:c07] elapsed time of MYCGRectmake: 14.443198


I guess that's why Apple didn't bother with that more difficult-to-read code--it was no faster.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home