strlen的实现是通过4个字节4个字节进行枚举,然后通过位运算来判断这4个字节中是否有一个字节含有0,这样的话,效率就提高了4倍。

这个效率提高是假设a&b&c&d与a&b有差不多效率的前提下。

那用8字节8字节来偏移的话,是不是更快呢?32位机上不会,64位机上会提高一倍。因为a&b在64位下会提高一倍,因为32位的寄存器大小是32位的,对于分别MOV高位与低位两次。

本来实验a&b&c&d与a&b的速度的,经实验验证,这两个效率确实是差不多的,然后去看汇编,看指令条数,在没有使用-O优化下,指令的条数差别跟运算符号的个数的倍数相同,就让我感到疑惑了。

下面附上实验的代码:

#include <iostream>
#include <time.h>
#include <cstdio>
#include <string>
using namespace std; int _strlen(const char *str) {
const unsigned int *p = (const unsigned int *) str;
unsigned int low = 0x01010101;
unsigned int high = 0x80808080;
while (true) {
unsigned int d = *p++;
if (((d - low) & ~d & high) != ) { // handle [0...256)
//if (((d - low) & high) != 0) { // handle [0...128)
break;
}
}
const char *q = (const char *)(p - );
for (int i = ; i < (int)sizeof(unsigned int); i++) {
if (q[i] == ) {
return q - str + i;
}
}
return -;
} int _strlen2(const char *str) {
const char *p = str;
while (*p != ) {
p++;
}
return p - str;
} int _strlen3(const char *str) {
const unsigned long long *p = (const unsigned long long *) str;
unsigned long long low = 0x0101010101010101;
unsigned long long high = 0x8080808080808080;
while (true) {
unsigned long long d = *p++;
if (((d - low) & ~d & high) != ) { // handle [0...256)
//if (((d - low) & high) != 0) { // handle [0...128)
break;
}
}
const char *q = (const char *)(p - );
for (int i = ; i < (int)sizeof(unsigned long long); i++) {
if (q[i] == ) {
return q - str + i;
}
}
return -;
} size_t _strlen4(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) - )) != ;
++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) > )
{
/* 64-bit version of the magic. */
/* Do the shift in two steps to avoid a warning if long has 32 bits. */
himagic = ((himagic << ) << ) | himagic;
lomagic = ((lomagic << ) << ) | lomagic;
}
/*j
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) != )
{
/* 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 - ); if (cp[] == )
return cp - str;
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
if (sizeof (longword) > )
{
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
}
}
}
} string gen_data() {
string a;
for (int i = ; i < ; i++) {
a.push_back('a');
}
return a;
} double get_run_time(int(*fp)(const char *), const char *str, int count) {
clock_t start = clock();
for (int i = ; i < count; i++) {
fp(str);
}
clock_t end = clock();
return (double)(end - start) / CLOCKS_PER_SEC;
} double get_run_time(size_t(*fp)(const char *), const char *str, int count) {
clock_t start = clock();
for (int i = ; i < count; i++) {
fp(str);
}
clock_t end = clock();
return (double)(end - start) / CLOCKS_PER_SEC;
} int main() {
string a = gen_data();
printf("%d\n", _strlen(a.c_str()));
printf("%d\n", _strlen2(a.c_str()));
printf("%d\n", _strlen3(a.c_str()));
printf("%d\n", (int)strlen(a.c_str()));
double time = get_run_time(&_strlen, a.c_str(), );
printf("%f\n", time);
double time2 = get_run_time(&_strlen2, a.c_str(), );
printf("%f\n", time2);
double time3 = get_run_time(&_strlen3, a.c_str(), );
printf("%f\n", time3);
double time4 = get_run_time(&strlen, a.c_str(), );
printf("%f\n", time4);
double time5 = get_run_time(&_strlen4, a.c_str(), );
printf("%f\n", time5);
}

关于strlen的更多相关文章

  1. php的empty(),trim(),strlen()方法

    如果empty()函数的参数是非空或非零的值,则empty()返回FALSE.换句话说,"".0."0".NULL.array().var$var:以及没有任何 ...

  2. c/c++中关于sizeof、strlen的使用说明

    sizeof: 一般指类型.变量等占用的内存大小(由于在编译时计算,因此sizeof不能用来返回动态分配的内存空间的大小) strlen: c字符串的长度(参数必须是字符型指针 char*,当数组名作 ...

  3. [PHP源码阅读]strlen函数

    文章来自:http://www.hoohack.me/2016/02/22/phps-source-analytics-strlen 我在github有对PHP源码更详细的注解.感兴趣的可以围观一下, ...

  4. php每天一题:strlen()与mb_strlen()的作用分别是什么

    strlen()与mb_strlen()都是用于获取字符串长度的,那么它们两个有什么不同? strlen()与mb_strlen()的不同之处在于mb_strlen()第二个参数可以用于指定字符编码. ...

  5. sizeof与strlen的区别

    1 sizeof是操作符,而strlen是库函数: 2 sizeof的参数可以为任意变量或类型,而strlen必须以char*做参数,且字符串必须以‘/0’结尾: 3 数组名用作sizeof参数时不会 ...

  6. strlen()和sizeof()求数组长度

    在字符常量和字符串常量的博文里有提: 求字符串数组的长度 标准库函数strlen(s)可以返回字符串s的长度,在头文件<string.h>里. strlen(s)的判断长度的依据是(s[i ...

  7. Linux C 字符串函数 strlen()、strcat()、strncat()、strcmp()、strncmp()、strcpy()、strncpy() 详解

      strlen(返回字符串长度) 表头文件 #include <string.h> 定义函数 size_t strlen(const char *s); 函数说明 strlen()用来计 ...

  8. 回文字符串的判断!关于strlen(char * str)函数

    #include <stdio.h> #include <string.h> int ishuiw(char * p); int main() { ;//true-false接 ...

  9. 关于strlen误用的一点记录

    今天帮一个朋友查一个错误,是运行时报vector iterator incompatible,一般这种问题是向量和迭代器的类型不兼容,或者是进行迭代器判等时前后向量的结构发生变化,如erase操作之后 ...

  10. sizeof、strlen、字符串、数组,整到一块,你还清楚吗?

    写在前面 sizeof.strlen.字符串.数组,提到这些概念,相信学过C语言的人都能耳熟能详,也能谈得头头是道,但是,在实际运用中,当这些内容交织在一起时,大家却不一定能搞地清清楚楚,本文的目的正 ...

随机推荐

  1. cocos2dx中导演的职责有哪些?

    1.一个游戏里面只有一个导演,因此采用了单例的设计模式,用getInstance方法来获得 2.游戏中导演负责openGL ES的初始化,场景的切换,游戏的暂停继续(相当于拍电影的ka),节点坐标与世 ...

  2. Linux 硬盘分区、分区、删除分区、格式化、挂载、卸载

    Linux 虽然一直都有在玩,但是对硬盘操作确实不是很熟悉今天有空,就整理了下. 1,创建分区 先查看下是否有磁盘没有分区 fdisk -l 其中第一个框和第二个框,是已经分好区的磁盘,第三个硬盘没有 ...

  3. Cannot install ubuntu or other linux flavours on citrix Xen server

    Citrix Xen sucks! When u try to install linux stuff on its Xen servers, u will get an error complain ...

  4. Python大数据依赖包安装

    一.安装 先安装python2.7.6,win下的numpy这些包需要直接匹配版本,然后安装“numpy-1.8.1-win32-superpack-python2.7”和“scipy-0.16.0- ...

  5. html5离线存储

    为了提升Web应用的用户体验,我们在做网站或者webapp的时候,经常需要保存一些内容到本地.例如,用户登录token,或者一些不经常变动的数据. 随着HTML5的到来,出现了诸如AppCache.l ...

  6. 15万甚至30万以内的SUV值不值得买?

    大家好,这个帖子比较长,也是我一直以来长期实践.思考.验证的结论,不当之处还请指正,也欢迎大家来共 同讨论,已经买了此价位SUV的战友们,看完后也不要生气,毕竟我的出发点是注重行车安全,人非神明,是个 ...

  7. 引擎设计跟踪(九.14.2h) 开发计划

    以后的开发计划: 完善game runtime code, 跑简单的demo目前只有编辑器的运行流程, 没有游戏/demo流程, 图形的测试主要在编辑器上测试, 现在需要测试android系统的图形, ...

  8. .NET设计模式(12):外观模式(Façade Pattern)(转)

    概述 在软件开发系统中,客户程序经常会与复杂系统的内部子系统之间产生耦合,而导致客户程序随着子系统的变化而变化.那么如何简化客户程序与子系统之间的交互接口?如何将复杂系统的内部子系统与客户程序之间的依 ...

  9. cts 测试环境安装 ubuntu

    1 下载cts测试包 和 sdk 包 http://source.android.com/compatibility/downloads.html  ----cts 包 http://develope ...

  10. javascript实现数据结构与算法系列:队列 -- 链队列和循环队列实现及示例

    1 队列的基本概念 队列(Queue):也是运算受限的线性表.是一种先进先出(First In First Out ,简称FIFO)的线性表.只允许在表的一端进行插入,而在另一端进行删除. 队首(fr ...