This loop is very slow, I think, because I create a lot of intermediate lines. How can I speed it up?

NSArray *splitPoints = [routeGeom componentsSeparatedByString:@"], ["];
routePoints = malloc(sizeof(CLLocationCoordinate2D) * ([splitPoints count] + 1));

int i=0;
NSArray *coords; 
for (NSString* coordStr in splitPoints) {

  coords = [coordStr componentsSeparatedByString:@","];

  routePoints[i].latitude = [[[coords objectAtIndex:0] substringFromIndex:1]floatValue];
  routePoints[i].longitude = [[coords objectAtIndex:1] floatValue];

  i++;

}
[coords release];

NSLog(@"** Time to split the route geometry into structs %f", [NSDate timeIntervalSinceReferenceDate] - start);
+1
source share
4 answers

Consider:

char *buf = [coordStr UTF8String];
sscanf(buf, "%f,%f", &routePoints[i].latitude, routePoints[i].longitude);
+6
source

I would think about using the returned c-string [coordStr UTF8String] and manual character analysis.

+2
source

, NSScanner . -SeparatedByString -substringFromIndex , .

+2

, , [coords release] ( ). , -GC-. coords, .

+2
source

Source: https://habr.com/ru/post/1717215/


All Articles