另一个速度提升可能? [英] Another Speed Boost Possible?

查看:89
本文介绍了另一个速度提升可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢此问题的受访者(这个循环非常慢,我想是因为我创建了很多中间字符串,我如何加快它?)我能够加速我的代码许多订单。



我想我可以做得更好。是否有可能避免在这里创建一堆NSString,而是将大的NSString(routeGeom)分成一堆char缓冲区并通过这些缓冲区进行迭代?



I从来没有做过任何C编程,所以如果你知道如何做到这一点,这将是非常感激。

  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 ++;

}


解决方案

  char * buf = [routeGeom UTF8String]; 
int bestGuess = 1<< (随你);
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) - &
}



为<$ c选择一个好的起始值$ c>(无论如何),以便2 无论代表路线中的平均点数。如果你不能,你可以尝试根据字符串的长度猜数字。否则,如果你想要精确,你可以解析字符串两次,首先计数,然后创建 routePoints ,然后解析数据,在这种情况下,你不需要



另一个选项。这假设CLLocationCoordinate2D只是一个2个浮点数的结构,与字符串中的数据顺序相同。

  char * buf = [routeGeom UTF8String]; 
int bestGuess = 1<< (随你);
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;


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

I think I can probably do a bit better though. Is it possible to avoid the creation of a bunch of NSString's here, and instead split the big NSString (routeGeom) into a bunch of char buffers and iterate through those?

I have never done any C programming, so if you know how to get this done, it would be much appreciated!

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++;

}

解决方案

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);
}


Pick a good starting value for (whatever) so that 2whatever is representative of an average number of points in a route. If you can't, you could try guessing the number based on the length of the string. Otherwise, if you want to be exact, you could parse the string twice, first counting, then create routePoints, then parse the data, in which case you wouldn't need the realloc section.

Edit:

Another option. This assumes that CLLocationCoordinate2D is simply a struct of 2 floats, in the same order as the data in the string.

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;

这篇关于另一个速度提升可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆