我整理了一个基本的分析应用程序。我尝试了你的两种方法,Rob Mayoff的两种方法。
stringWithFormat:
取平均值为0.000008秒。其他三种方法(呼叫stringValue
在numberWithLongLong:
)和罗布的两个,均平均分别为0.000011秒。
这是我的代码。这显然不是100%准确的,因为有包括配置文件中的一些其他的操作,但差异将在所有测试的一样:
- (void) startProfiling {
self.startDate = [NSDate date];
}
- (NSTimeInterval) endProfiling {
NSDate *endDate = [NSDate date];
NSTimeInterval time = [endDate timeIntervalSinceDate:self.startDate];
self.startDate = nil;
NSLog(@"seconds: %f", time);
return time;
}
- (void)doTest:(id)sender {
long long val = 1234567890987654321;
NSTimeInterval totalTime = 0;
for (int i = 0; i < 1000; i++) {
[self startProfiling];
// change this line for each test
NSString* str = [NSString stringWithFormat:@"%lld", val];
totalTime += [self endProfiling];
}
NSLog(@"average time: %f", totalTime/1000);
}
有多快与方法1去的速度有多快,你试图得到? – CaptJak
我不认为差异值得担心。 –
您是否需要立即拥有所有的字符串,或者您是否可以重复使用可变字符串?你确定这条线是性能问题吗? –