[英]Why is this C-style code 10X slower than this obj-C style code?
//obj C version, with some - less than one second on 18,000 iterations // obj C版本,有些 - 在18,000次迭代中不到一秒
for (NSString* coordStr in splitPoints) {
char *buf = [coordStr UTF8String];
sscanf(buf, "%f,%f,", &routePoints[i].latitude, &routePoints[i].longitude);
i++;
}
//C version - over 13 seconds on 18,000 iterations // C版 - 在18,000次迭代中超过13秒
for (i = 0; buf != NULL; buf = strchr(buf,'['), ++i) {
buf += sizeof(char);
sscanf(buf, "%f,%f,", &routePoints[i].latitude, &routePoints[i].longitude);
}
As a corollary question, is there any way to make this loop faster? 作为一个推论问题,有没有办法让这个循环更快?
Also see this question: Another Speed Boost Possible? 另见这个问题: 另一种速度提升可能吗?
Measure, measure, measure. 测量,测量,测量。
Measure the code with the Sampler instrument in Instruments. 使用仪器中的采样器仪器测量代码。
With that said, there is an obvious inefficiency in the C code compared to the Objective-C code. 话虽如此,与Objective-C代码相比,C代码明显效率低下。
Namely, fast enumeration -- the for(x in y)
syntax -- is really fast and, more importantly, implies that splitPoints
is an array or set that contains a bunch of data that has already been parsed into individual objects. 也就是说,快速枚举 - for(x in y)
语法 - 非常快,更重要的是,暗示splitPoints
是一个数组或集合,其中包含已经解析为单个对象的一堆数据。
The strchr()
call in the second loop implies that you are parsing stuff on the fly. 第二个循环中的strchr()
调用意味着您正在动态解析内容。 In and of itself, strchr()
is a looping operation and will consume time, more-so as the # of characters between occurrences of the target character increase. 就其本身而言, strchr()
是一个循环操作,并且会消耗时间,因为目标字符出现之间的字符数增加。
That is all conjecture, though. 但这都是猜想。 As with all optimizations, speculation is useless and gathering concrete data using the [rather awesome] set of tools provided is the only way to know for sure. 与所有优化一样,推测是无用的,使用提供的[相当棒的]工具集合收集具体数据是确定的唯一方法。
Once you have measured, then you can make it faster. 一旦测量完毕,就可以加快速度。
Having nothing to do with performance, your C code has an error in it. 与性能无关,您的C代码中存在错误。 buf += sizeof(char)
should simply be buf++
. buf += sizeof(char)
应该只是buf++
。 Pointer arithmetic always moves in units the size of the type. 指针算术始终以类型的大小为单位移动。 It worked fine in this case because sizeof(char) was 1. 在这种情况下它工作得很好,因为sizeof(char)是1。
Obj C code looks like it has precomputed some split points, while the C code seeks them in each iteration. Obj C代码看起来像预先计算了一些分裂点,而C代码在每次迭代中寻找它们。 Simple answer? 简单回答? If N is the length of buf
and M the number of your split points, it looks like your two snippets have complexities of O(M) versus O(N*M); 如果N是buf
的长度而M是你的分裂点的数量,看起来你的两个片段有O(M)与O(N * M)的复杂性; which one's slower? 哪一个慢?
edit: Really amazed me though, that some would think C code is axiomatically faster than any other solution. 编辑:我真的很惊讶,有些人会认为C代码在公理上比任何其他解决方案都快。
Vectorization can be used to speed up C code. 矢量化可用于加速C代码。
Example: 例:
Even faster UTF-8 character counting 甚至更快的UTF-8字符计数
(But maybe just try to avoid the function call strchr() in the loop condition.) (但也许只是试着在循环条件中避免函数调用strchr()。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.