strlen 函数 源码分析,strlen 很快的原因。

strlen_test.cpp

#include<iostream>
#include<cstring>
#include<time.h>

using namespace std;

/自定义strlen

int my_strlen(char * str){
char* start = str;
while(*(++str) !='\0') ;;
return str-start;
}

 

int main(){
clock_t startTime,endTime;
startTime = clock();
char * str = "nsfsfgdgsdgdsgdfgdfgdfg";
for(int i = 0;i< 1000000;i++)
my_strlen(str);
endTime = clock();
cout<< "my strlen function run time:"<<endTime-startTime<<endl;

startTime = clock();
for(int i = 0;i< 1000000;i++)
strlen(str);
endTime = clock();
cout<< "system strlen function run time:"<<endTime-startTime<<endl;

}

//======================================调用的库函数源码======================================

库函数源码: 主要思想是每次在内存中拿 unsigned long int  大小的数据,然后比较这段4个或者8个字节的数据中的每个char 是否为 空字符。

#include <string.h>

#include <stdlib.h>

#undef strlen

#ifndef STRLEN
# define STRLEN strlen
#endif

/* Return the length of the null-terminated string STR. Scan for
the null terminator quickly by testing four bytes at a time. */
size_t
STRLEN (const char *str)
{
const char *char_ptr;
const unsigned long int *longword_ptr;    
unsigned long int longword, himagic, lomagic;      

/* Handle the first few characters by reading one character at a time.
Do this until CHAR_PTR is aligned on a longword boundary. */
for (char_ptr = str; ((unsigned long int) char_ptr 

/* 将字符串指针挪到一个long int 地址对齐的位置上面,long int 在不同的编译器环境下其所占用的内存大小 有可能是4 ,也有可能是8.) 。这里与3或者7 做位与运算,当结果为0时,字符串指针必然long int 类型对齐。*/
& (sizeof (longword) - 1)) != 0;
++char_ptr)
if (*char_ptr == '\0')
return char_ptr - str;

/* All these elucidatory comments refer to 4-byte longwords,
but the theory applies equally well to 8-byte longwords. */

longword_ptr = (unsigned long int *) char_ptr;

/* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
the "holes." Note that there is a hole just to the left of
each byte, with an extra at the end:

bits: 01111110 11111110 11111110 11111111
bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD

The 1-bits make sure that carries propagate to the next 0-bit.
The 0-bits provide holes for carries to fall into. */
himagic = 0x80808080L;
lomagic = 0x01010101L;
if (sizeof (longword) > 4)
{
/* 64-bit version of the magic. */
/* Do the shift in two steps to avoid a warning if long has 32 bits. */
himagic = ((himagic << 16) << 16) | himagic;
lomagic = ((lomagic << 16) << 16) | lomagic;
}
if (sizeof (longword) > 8)
abort ();

/* Instead of the traditional loop which tests each character,
we will test a longword at a time. The tricky part is testing
if *any of the four* bytes in the longword in question are zero. */
for (;;)
{
longword = *longword_ptr++;

if (((longword - lomagic) & ~longword & himagic) != 0)
{
/* Which of the bytes was the zero? If none of them were, it was
a misfire; continue the search. */

const char *cp = (const char *) (longword_ptr - 1);

if (cp[0] == 0)
return cp - str;
if (cp[1] == 0)
return cp - str + 1;
if (cp[2] == 0)
return cp - str + 2;
if (cp[3] == 0)
return cp - str + 3;
if (sizeof (longword) > 4)
{
if (cp[4] == 0)
return cp - str + 4;
if (cp[5] == 0)
return cp - str + 5;
if (cp[6] == 0)
return cp - str + 6;
if (cp[7] == 0)
return cp - str + 7;
}
}
}
}

 //======================================================================================================================

g++ strlen_test.cpp -o strlen_test  

./strlen_test

运行结果:

my strlen function run time:49467
system strlen function run time:2128

 

 

 

为了方便调试: 自己改了下测试代码:(库函数源码拿来重新命名下,重新编译下,这样方便调试库函数了),当然使用G++编译测试代码的时候需要将C代码函数做外部C代码声明)

strlen_test.cpp

#include<iostream>
//#include<cstring>
#include<time.h>
using namespace std;

int my_strlen( char * str){
char* start = str;
while(*(++start) != '\0') ;
return start-str;
}

extern "C"{
unsigned long int strlen1(char *str);
}

int main(){
int len1,len2;
clock_t startTime,endTime;
startTime = clock();
char * str = "nsfsfgdgsdgdsgdfgdfgdfg";
for(int i = 0;i< 10000;i++)
len1 = my_strlen(str);
endTime = clock();
cout<< "my strlen function run time:"<<endTime-startTime<<endl;

startTime = clock();
for(int i = 0;i< 10000;i++)
len2 = strlen1(str);
endTime = clock();
cout<< "system strlen function run time:"<<endTime-startTime<<endl;
cout<<len1<<" " <<len2<<endl;
}

strlen.c 

#include <string.h>
#include <stdlib.h>

#undef strlen

#ifndef STRLEN
# define STRLEN strlen1
#endif

/* Return the length of the null-terminated string STR. Scan for
the null terminator quickly by testing four bytes at a time. */
size_t
STRLEN (const char *str)
{
const char *char_ptr;
const unsigned long int *longword_ptr;
unsigned long int longword, himagic, lomagic;

/* Handle the first few characters by reading one character at a time.
Do this until CHAR_PTR is aligned on a longword boundary. */
for (char_ptr = str; ((unsigned long int) char_ptr
& (sizeof (longword) - 1)) != 0;
++char_ptr)
if (*char_ptr == '\0')
return char_ptr - str;

/* All these elucidatory comments refer to 4-byte longwords,
but the theory applies equally well to 8-byte longwords. */

longword_ptr = (unsigned long int *) char_ptr;

/* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
the "holes." Note that there is a hole just to the left of
each byte, with an extra at the end:

bits: 01111110 11111110 11111110 11111111
bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD

The 1-bits make sure that carries propagate to the next 0-bit.
The 0-bits provide holes for carries to fall into. */
himagic = 0x80808080L;
lomagic = 0x01010101L;
if (sizeof (longword) > 4)
{
/* 64-bit version of the magic. */
/* Do the shift in two steps to avoid a warning if long has 32 bits. */
himagic = ((himagic << 16) << 16) | himagic;
lomagic = ((lomagic << 16) << 16) | lomagic;
}
if (sizeof (longword) > 8)
abort ();

/* Instead of the traditional loop which tests each character,
we will test a longword at a time. The tricky part is testing
if *any of the four* bytes in the longword in question are zero. */
for (;;)
{
longword = *longword_ptr++;

if (((longword - lomagic) & ~longword & himagic) != 0)
{
/* Which of the bytes was the zero? If none of them were, it was
a misfire; continue the search. */

const char *cp = (const char *) (longword_ptr - 1);

if (cp[0] == 0)
return cp - str;
if (cp[1] == 0)
return cp - str + 1;
if (cp[2] == 0)
return cp - str + 2;
if (cp[3] == 0)
return cp - str + 3;
if (sizeof (longword) > 4)
{
if (cp[4] == 0)
return cp - str + 4;
if (cp[5] == 0)
return cp - str + 5;
if (cp[6] == 0)
return cp - str + 6;
if (cp[7] == 0)
return cp - str + 7;
}
}
}
}

gcc -c strlen.c
g++ -c strlen_test.cpp
g++ -g -o strlen_test strlen_test.o strlen.o
./strlen_test
rm -f strlen.o strlen_test.o strlen_test

运行结果:

my strlen function run time:51580
system strlen function run time:26504

遗留问题:

1. 自己编译和调用的只是改了个函数名的同样的代码为什么还是和库函数有指数级别的速度差距,虽然比我简单写的strlen 好一些?

2. 编译和运行环境都是在阿里云上面进行的,是否是云计算的环境影响了执行的速度?

3. 为什么C++程序链接C语言的函数时,需要先把C函数编译成.o 文件,不可以直接使用g++ strlen_test.cpp strlen.c -o strlen_test的形式?

posted @ 2022-06-16 02:02  danieldai  阅读(113)  评论(0编辑  收藏  举报