如何尽快实施strlen [英] How to implement strlen as fast as possible
问题描述
假设您正在使用x86 32位系统。
Assume that you're working a x86 32-bits system. Your task is to implement the strlen as fast as possible.
您需要注意两个问题:
1.地址对齐。
2.读取具有机器字长(4个字节)的内存。
There're two problems you've to take care: 1. address alignment. 2. read memory with machine word length(4 bytes).
在给定的字符串中找到第一个对齐地址并不难。
It's not hard to find the first alignment address in the given string.
然后,我们可以用4个字节读取一次内存,并计算其总长度。但是,一旦4个字节中有一个零字节,我们就应该停止,并在零字节之前算出剩余字节。为了快速检查零字节,glibc提供了一个代码段:
Then we can read memory once with the 4 bytes, and count up it the total length. But we should stop once there's a zero byte in the 4 bytes, and count the left bytes before zero byte. In order to check the zero byte in a fast way, there's a code snippet from glibc:
unsigned long int longword, himagic, lomagic;
himagic = 0x80808080L;
lomagic = 0x01010101L;
// There's zero byte in 4 bytes.
if (((longword - lomagic) & ~longword & himagic) != 0) {
// do left thing...
}
我在Visual C ++中使用它来与CRT的实现进行比较。 CRT的速度比上述CRT快得多。
I used it in Visual C++, to compare with CRT's implementation. The CRT's is much more faster than the above one.
我对CRT的实现并不熟悉,他们是否使用更快的方法来检查零字节?
I'm not familiar with CRT's implementation, did they use a faster way to check the zero byte?
推荐答案
第一个CRT的是直接直接用汇编器编写的。您可以在此处查看其源代码 C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\intel\strlen.asm
(这是VS 2008的版本)
First CRT's one is written directly in assembler. you can see it's source code here C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\intel\strlen.asm
(this is for VS 2008)
这篇关于如何尽快实施strlen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!