快点走? [英] faster strlen?
问题描述
典型的 strlen()
从第一个字符开始遍历,直到找到 \0
。
这要求您遍历每个字符。
在算法意义上,它的O(N)。
Typical strlen()
traverse from first character till it finds \0
.
This requires you to traverse each and every character.
In algorithm sense, its O(N).
在模糊定义输入的地方,有没有更快的方法可以做到这一点。
像:长度小于50,或者长度大约200个字符。
Is there any faster way to do this where input is vaguely defined. Like: length would be less than 50, or length would be around 200 characters.
我想到了查找块,但没有得到任何优化。
I thought of lookup blocks and all but didn't get any optimization.
推荐答案
实际上, strlen
的glibc实现是矢量化方法的一个有趣示例。它的独特之处在于它不使用向量指令,而是找到一种仅对缓冲区中32位或64位字使用普通指令的方法。
Actually, glibc's implementation of strlen
is an interesting example of the vectorization approach. It is peculiar in that it doesn't use vector instructions, but finds a way to use only ordinary instructions on 32 or 64 bits words from the buffer.
这篇关于快点走?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!