如何尽快实施strlen

编程入门 行业动态 更新时间:2024-10-24 22:23:36
本文介绍了如何尽快实施strlen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设您正在使用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

本文发布于:2023-07-30 00:41:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1245022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:strlen

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!