Why is this 10X C-style code slower than this obj-C style code?

// obj C version, with some less than one second in 18,000 iterations

for (NSString* coordStr in splitPoints) {

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

  i++;

}

// version C - more than 13 seconds at 18,000 iterations

for (i = 0; buf != NULL; buf = strchr(buf,'['), ++i) {

  buf += sizeof(char);
  sscanf(buf, "%f,%f,", &routePoints[i].latitude, &routePoints[i].longitude);

}

As an investigative question, is there a way to make this cycle faster?

Also see this question: Is another speed increase possible?

+3
source share
4 answers

Measurement, measurement, measurement.

Measure the code using the Sampler tool in Tools.

With that said, the inefficiency of C code is obvious compared to Objective-C code.

, - for(x in y) - , , , splitPoints - , , .

strchr() , " ". strchr() , , .

, . , [ ] , .

, , .

+10

, C . buf += sizeof(char) buf++. . , sizeof (char) 1.

+3

Obj C , , C . ? N - buf M - , , O (M) O (N * M); ?

edit: , , C- .

+1

Vectorization can be used to speed up C code.

Example:

UTF-8 even faster character counting

(But maybe just try to avoid calling strchr () in a loop condition.)

0
source

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


All Articles