NSXMLParser响应时间

时间:2014-05-19 13:28:26

标签: ios parsing nsxmlparser

我如何获得解析器请求的响应时间?

NSString *connString = [NSString stringWithFormat:@""];

NSURL *url = [[NSURL alloc] initWithString:connString];
NSData *data = [NSData dataWithContentsOfURL:url];

parserUserData = [[NSXMLParser alloc] initWithData:data];
[parserUserData setDelegate:self];

我使用了以下示例,但它没有返回响应时间,只是响应。

NSError *error = nil;
NSString *string = [[NSString alloc] initWithContentsOfURL:url
                                                  encoding:NSUTF8StringEncoding
                                                     error:&error];

if (error != nil)
{
    NSLog(@"error: %@", error);
}
else
{
    NSLog(@"response: %@", string);
}

1 个答案:

答案 0 :(得分:0)

因为解析是同步进行的,所以你可以在解析之前捕获开始时间,然后计算完成后经过的时间:

CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();

NSString *connString = [NSString stringWithFormat:@"..."];

NSURL *url = [[NSURL alloc] initWithString:connString];
NSData *data = [NSData dataWithContentsOfURL:url];

CFTimeInterval elapsed = CFAbsoluteTimeGetCurrent() - start;

NSLog(@"dataWithContentsOfURL elapsed time: %.3f", elapsed);

parserUserData = [[NSXMLParser alloc] initWithData:data];
[parserUserData setDelegate:self];

[parserUserData parse];

elapsed = CFAbsoluteTimeGetCurrent() - start;

NSLog(@"dataWithContentsOfURL & parse elapsed time: %.3f", elapsed);