我試圖從iPhone瞭解3G網絡上的NSURLConnection
性能。我有以下測試代碼NSURLConnection優化
-(void)doTest2 {
max = 5;
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request2 = [[[NSURLRequest alloc] initWithURL:url] autorelease];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request2 delegate:self];
self.startDate = [NSDate date];
if (conn)
{
receivedData = [[NSMutableData data] retain];
} }
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.endDate = [NSDate date];
NSTimeInterval interval = [self.endDate timeIntervalSinceDate:self.startDate];
NSLog(@"Time:%f Size:%u", interval, [receivedData length]);
[receivedData release];
count = count + 1;
if (count == max) {
count = 0;
sleep(3);
}
self.doTest2;}
第一個請求很慢(超過1秒)。請求2-5很快(小於.25秒)。如果我睡了3秒或更長時間,睡眠後的第一個請求很慢。但是如果我睡了不到3秒,速度很快。任何想法爲什麼?