如何编写更好的 strlen 函数?
我正在阅读“Write Great Code Volume 2”,它显示了以下 strlen 实现:
int myStrlen( char *s )
{
char *start;
start = s;
while( *s != 0 )
{
++s;
}
return s - start;
}
书上说这种实现对于没有经验的 C 程序员来说是典型的。过去 11 年我一直在用 C 编写代码,但我不知道如何用 C 编写比这更好的函数(我可以考虑用汇编编写更好的东西)。怎么可能用 C 写出比这更好的代码呢?我查看了 glibc 中 strlen 函数的标准库实现,但我无法理解其中的大部分内容。在哪里可以找到有关如何编写高度优化的代码的更好信息?
I am reading "Write Great Code Volume 2" and it shows the following strlen impelementation:
int myStrlen( char *s )
{
char *start;
start = s;
while( *s != 0 )
{
++s;
}
return s - start;
}
the book says that this implementation is typical for an inexperienced C programmer. I have been coding in C for the past 11 years and i can't see how to write a function better than this in C(i can think of writing better thing in assembly). How is it possible to write code better than this in C? I looked the standard library implementation of the strlen function in glibc and I couldn't understand most part of it. Where can I find better information on how to write highly optimized code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自优化 strlen(),Colm MacCarthaigh 的博文:
它给出了一个很好的例子,告诉你可以朝什么方向努力来加快速度。以及其中的另一句话
From Optimising strlen(), a blogpost by Colm MacCarthaigh:
It gives good example in what direction you can work to speed it up. And another quote from it
维克多,看看这个:
http://en.wikipedia.org/wiki/Strlen#Implementation
PS 你的原因不明白 glibc 版本可能是因为它使用位移位来查找 \0。
Victor, take a look at this:
http://en.wikipedia.org/wiki/Strlen#Implementation
P.S. The reason you don't understand the glibc version is probably because it uses bit shifting to find the \0.
对于初学者来说,这对于像 UTF-8 这样的编码来说毫无价值……也就是说,计算 UTF-8 字符串中的字符数更复杂,而字节数当然就像计算一样容易,比方说,一个 ASCII 字符串。
一般来说,您可以通过读取更大的寄存器来在某些平台上进行优化。由于到目前为止发布的其他链接没有相关示例,因此这里有一些低端字节序的伪代码:
For starters, this is worthless for encodings like UTF-8... that is, calculating the number of characters in an UTF-8 string is more complicated, whereas the number of bytes is, of course, just as easy to calculate as in, say, an ASCII string.
In general, you can optimize on some platforms by reading into larger registers. Since the other links posted so far don't have an example of that, here's a bit of pseudo-pseudocode for lower endian:
正如其他人指出的那样,更快的算法会读取整个单词而不是单个字符,并使用按位操作来找到终止空值。如果您采用这种方法,请注意字对齐指针,因为某些 CPU 架构不允许您从未对齐的地址读取字(即使在不需要对齐的架构上,这也是触发段错误的好方法)。
底线:
优秀的代码在除对性能最关键的情况外的所有情况下都强调可读性而不是速度。尽可能清晰地编写代码,并且只优化被证明是瓶颈的部分。
As others have pointed out, a faster algorithm reads entire words instead of individual characters and uses bitwise operations to find the terminating null. Be mindful of word-aligning your pointer if you take this approach, as some CPU architectures won't let you read words from an unaligned address (and it's a great way to trigger a segfault even on architectures that don't require alignment).
Bottom line:
Great code emphasizes readability over speed in all but the most performance-critical cases. Write your code as clearly as you can and only optimize the parts that prove to be bottlenecks.
读取与机器数据总线大小不同的变量的成本很高,因为机器只能读取该大小的变量。因此,每当请求不同大小(假设更小)的东西时,机器必须做一些工作以使其看起来像请求大小的变量(例如移位位)。
因此,您最好以机器大小的字读取数据,然后使用 AND 运算检查是否有 0。另外,在扫描字符串时,请确保从对齐的起始地址开始。
Reading a variable that is not of the same size as the machine data bus size is expensive, because the machine can only read variables of that size. Therefore, whenever something of different size (let's say smaller) is requested, the machine must do work to make it look like a variable of the requested size (like shifting the bits).
So you better read the data in machine sized words, and then use the AND operation to check for 0s. Also, when scanning through the string, make sure you start at an aligned start address.
回答OP关于在哪里可以找到如何编写性能代码的建议的问题,这里是 链接 麻省理工学院关于编写优化 C 代码的开放课程(查找页面左侧的“材料”链接)。
Answering OP's question about where to find suggestions how to write code for performance, here's link to MIT OpenCourse on writing Optimized C Code (look for "Materials" link on the left of page).
以下应该比朴素算法更快并且适用于 32/64 位。
The following should be faster than the naive algorithm and work for 32/64 bit.