单元测试计算耗时函数
static NSMutableArray* performanceProfiles = nil; + (void) performanceProfileStart{ if (performanceProfiles == nil) { performanceProfiles = [[NSMutableArray alloc] init]; } [performanceProfiles addObject:[NSDate date]]; } + (void) performanceProfileEnd:(NSString *)message{ NSDate* start = [performanceProfiles objectAtIndex: performanceProfiles.count-1]; NSLog(@"[%.3f] %@", -[start timeIntervalSinceNow], message); [performanceProfiles removeLastObject]; } |
使用
int testTimes = 1000; [UtilTests performanceProfileStart]; for(int i=0; i<testTimes; i++)[Util func1]; [UtilTests performanceProfileEnd:@"func1 output message"]; [UtilTests performanceProfileStart]; for(int i=0; i<testTimes; i++)[Util func2]; [UtilTests performanceProfileEnd:@"func2 output message"]; |