开发中经常要观察函数执行时间,以便于调优。宏定义是一个快捷办法。
#define START() NSDate *startTime = [NSDate date]
#define STOP() NSLog(@" took time: %.0f ms", -[startTime timeIntervalSinceNow] * 1000); startTime = [NSDate date]
- (void)test {
START();
for (int i = 0; i < 10000; i++)
{
int r = 100;
int x = arc4random() % r;
int y = arc4random() % r;
BOOL result = [self point:CGPointMake(x, y) isInCircle:60];
NSLog(@"%d-- x= %d:y=%d",result,x,y);
}
STOP();
}
使用示例
- (BOOL)point:(CGPoint)point isInCircle:(CGFloat)radius
{
//hypotf() pow()
CGFloat r2 = radius * radius;
CGFloat x2 = point.x * point.x;
CGFloat y2 = point.y * point.y;
BOOL ret = r2 >= x2+y2;
// BOOL ret = radius >= hypotf(point.x, point.y);
return ret;
}