I’m having a bit of trouble figuring out why this code is running slow. What I’m doing below is getting JSON data from yahoo finance for 4 companies. From the JSON data I’m simply extracting the names of the 4 companies. However as I NSLog the names of the 4 companies it takes almost 2 full seconds to do so! Is their something in the code that I’m doing wrong? how can I get the code to run faster?
for (int i=0; i<4; i++) {
//download JSON data
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22,%22GOOG%22,%22GE%22,%22MCD%22)%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json"]];
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data //1
options:kNilOptions
error:&error];
//Get the relavent data from JSON
NSString* companyName = [[[[[json objectForKey:@"query"] objectForKey:@"results"] objectForKey:@"quote"] objectAtIndex:i] objectForKey:@"Name"] ;
NSLog(@"company name is %@", companyName);
}
As Richard said, don’t download the file 4 times. As a first iteration try this: