Is speed increase possible?

Thanks to the respondents on this question ( This cycle is very slow, I think, because I create a lot of intermediate lines. How can I speed it up? ) I managed to speed up my code by many orders of magnitude.

I think I'll probably get a little better. Is it possible to avoid creating an NSString pool here and instead split a large NSString (routeGeom) into a bunch of char buffers and iterate through these?

I never programmed C, so if you know how to do this, we will be very grateful!

NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];

NSString *routeGeom = [pieces objectAtIndex:1];
NSArray *splitPoints = [routeGeom componentsSeparatedByString:@"],["];

routePoints = malloc(sizeof(CLLocationCoordinate2D) * ([splitPoints count] + 1));

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

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

  i++;

}
+2
source share
3 answers
char *buf = [routeGeom UTF8String];
int bestGuess = 1 << (whatever);
routePoints = malloc(sizeof(CLLocationCoordinate2D) * bestGuess);
for (int i = 0; buf != NULL; buf = strchr(buf+1,'['), ++i) {
  if (i >= bestGuess) {
      bestGuess <<= 1;
      routePoints = realloc(routePoints, sizeof(CLLocationCoordinate2D) * bestGuess);
  }
  sscanf(buf+1, "%f,%f,", &(routePoints + i)->latitude, &(routePoints + i)->longitude);
}


(whatever), 2 any . , , . , , , , routePoints, , realloc.

Edit:

. , CLLocationCoordinate2D 2 , .

char *buf = [routeGeom UTF8String];
int bestGuess = 1 << (whatever);
float *tmpFloats = (float *)malloc(sizeof(float) * bestGuess);
float *index = tmpFloats;
for (int i = 0; buf != NULL; buf = strchr(buf+1,'['), ++i, index += 2) {
  if (i >= bestGuess) {
    bestGuess <<= 1;
    tmpFloats = (float *)realloc(tmpFloats, sizeof(float) * bestGuess);
  }
  sscanf(buf+1, "%f,%f,", index, index + 1);
}
CLLocationCoordinate2D *routePoints = (CLLocationCoordinate2D *)tmpFloats;
+1

realloc, theres . , arrayname [index]. , ..

int array[5000];
int* intPointer = &array;
for(int i=0;i<5000;i++,intPointer++)
    *intPointer = something

&routePoints[i] "& pointPoints + * sizeof (CLLocationCoordinate2D)" .

C . .

, , , , C, ( ).

+1
0

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


All Articles