关于strlen
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的更多相关文章
- php的empty(),trim(),strlen()方法
如果empty()函数的参数是非空或非零的值,则empty()返回FALSE.换句话说,"".0."0".NULL.array().var$var:以及没有任何 ...
- c/c++中关于sizeof、strlen的使用说明
sizeof: 一般指类型.变量等占用的内存大小(由于在编译时计算,因此sizeof不能用来返回动态分配的内存空间的大小) strlen: c字符串的长度(参数必须是字符型指针 char*,当数组名作 ...
- [PHP源码阅读]strlen函数
文章来自:http://www.hoohack.me/2016/02/22/phps-source-analytics-strlen 我在github有对PHP源码更详细的注解.感兴趣的可以围观一下, ...
- php每天一题:strlen()与mb_strlen()的作用分别是什么
strlen()与mb_strlen()都是用于获取字符串长度的,那么它们两个有什么不同? strlen()与mb_strlen()的不同之处在于mb_strlen()第二个参数可以用于指定字符编码. ...
- sizeof与strlen的区别
1 sizeof是操作符,而strlen是库函数: 2 sizeof的参数可以为任意变量或类型,而strlen必须以char*做参数,且字符串必须以‘/0’结尾: 3 数组名用作sizeof参数时不会 ...
- strlen()和sizeof()求数组长度
在字符常量和字符串常量的博文里有提: 求字符串数组的长度 标准库函数strlen(s)可以返回字符串s的长度,在头文件<string.h>里. strlen(s)的判断长度的依据是(s[i ...
- Linux C 字符串函数 strlen()、strcat()、strncat()、strcmp()、strncmp()、strcpy()、strncpy() 详解
strlen(返回字符串长度) 表头文件 #include <string.h> 定义函数 size_t strlen(const char *s); 函数说明 strlen()用来计 ...
- 回文字符串的判断!关于strlen(char * str)函数
#include <stdio.h> #include <string.h> int ishuiw(char * p); int main() { ;//true-false接 ...
- 关于strlen误用的一点记录
今天帮一个朋友查一个错误,是运行时报vector iterator incompatible,一般这种问题是向量和迭代器的类型不兼容,或者是进行迭代器判等时前后向量的结构发生变化,如erase操作之后 ...
- sizeof、strlen、字符串、数组,整到一块,你还清楚吗?
写在前面 sizeof.strlen.字符串.数组,提到这些概念,相信学过C语言的人都能耳熟能详,也能谈得头头是道,但是,在实际运用中,当这些内容交织在一起时,大家却不一定能搞地清清楚楚,本文的目的正 ...
随机推荐
- python-面向对象(指数对象举例)
class Index(object): def __init__(self,index_name,index_code,closePrice_yesterday,closePrice_today): ...
- bzoj 1015 并查集
逆向思维,先将整张图以最后所有要求的点毁掉的状态建图,然后倒着 加点就行了,用并查集维护连通块 /*************************************************** ...
- boost之thread
1.boost里的thread创建之后会立即启动. 代码示例: #include <iostream> #include <string> #include <vecto ...
- 【BZOJ】【4002】【JLOI2015】有意义的字符串
构造线性递推式+矩阵乘法 题解戳PoPoQQQ 为了自己以后看的方便手打一遍好了>_> 求$( \frac{b+\sqrt{d}}{2} )^n$的整数部分对p取模后的值 其中$b\mod ...
- rivers ioi2005 树形dp
说句实话,写完这道题,很想吐一口血出来,以示我心情的糟糕: 题目很简单,树形dp,正常做30分钟,硬是做了好几个小时,真是伤心. 题解不写了,只是吐个槽,网上没有用背包写的dp,全是左儿子右兄弟写法, ...
- java数组与内存控制
1.1数组的初始化 数组是大多数编程语言都提供一种的复合结构,如果程序需要多个类型相同的变量时,就可以考虑定义一个数组.java语言的数组变量时引用类型的变量,因此具有java独有的特性. java数 ...
- StoreKit framework
关于In-APP Purchase 1. 中文文档 基本流程: http://www.cnblogs.com/wudan7/p/3621763.html 三种购买形式及StoreKit code介绍: ...
- Metasploit命令大全
Metasploit是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,并管理专家驱动的安全性进行评估,提供真正的安全风险情报.这些功能包括智能开发,密码审计, ...
- JavaScript事件委托的技术原理
如今的JavaScript技术界里最火热的一项技术应该是‘事件委托(event delegation)’了.使用事件委托技术能让你避免对特定的每个节点添加事件监听器:相反,事件监听器是被添加到它们的父 ...
- 驱动笔记 - IO端口和IO内存
访问IO端口 (#include <asm/io.h>) 设备资源struct resource{ resource_size_t start; //资源起始物理地址 resource_s ...