Iphone 我如何测试JSON web服务下载数据需要多长时间。

Iphone 我如何测试JSON web服务下载数据需要多长时间。,iphone,ios,objective-c,json,Iphone,Ios,Objective C,Json,如何测试从JSON web服务下载数据需要多长时间 目前我正在下载以下代码,并希望记录下载所需的时间 - (void)downloadData{ // Download the JSON NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryLine] encoding:NSStringE

如何测试从JSON web服务下载数据需要多长时间

目前我正在下载以下代码,并希望记录下载所需的时间

- (void)downloadData{

// Download the JSON
NSString *jsonString = [NSString
                        stringWithContentsOfURL:[NSURL URLWithString:queryLine]
                        encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
                        error:nil];

NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];

// Create parser for the api
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];

itemsTMP = [results objectForKey:@"results"];

[self setAllItems:[itemsTMP copy]];
self.displayItems = [itemsTMP copy];

int a =   [displayItems count];
    NSString *countString = [NSString stringWithFormat:@" results %d",a];
    countLabel.text = countString;

}

感谢您的帮助

您应该使用Instruments工具,它是Apple Developer工具的一部分。这真的很好,因为你完全按照自己的要求去做,还有很多其他的事情。这里是一个很好的起点:。

在下载JSON之前,先做一个“时间戳”

NSDate *startTime = [[NSDate alloc] init];

// Download the JSON
NSString *jsonString = [NSString
                        stringWithContentsOfURL:[NSURL URLWithString:queryLine]
                        encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
                        error:nil];

NSDate *elapsedTime = [NSDate dateWithTimeInterval:0 sinceDate:startTime];

elapsedTime将是一个NSDate对象,设置为从日期算起的秒。

这就是我最后使用的。它非常适合我的需要

  NSLog(@"Begin <your method name>");
NSDate *startTime = [NSDate date];

// Do something useful here

NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];
NSLog(@"End <your method name>");
NSLog([NSString stringWithFormat:@"Elapsed time: %f", -elapsedTime]);
NSLog(@“Begin”);
NSDate*startTime=[NSDate-date];
//在这里做些有用的事
NSTimeInterval elapsedTime=[startTime TimeIntervalencesInnow];
NSLog(@“结束”);
NSLog([NSString stringWithFormat:@“运行时间:%f”,-elapsedTime]);

猜测一下:下载数据并计时?