我整理了一個基本的分析應用程序。我嘗試了你的兩種方法,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
我不認爲差異值得擔心。 –
您是否需要立即擁有所有的字符串,或者您是否可以重複使用可變字符串?你確定這條線是性能問題嗎? –