关于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语言的人都能耳熟能详,也能谈得头头是道,但是,在实际运用中,当这些内容交织在一起时,大家却不一定能搞地清清楚楚,本文的目的正 ...
随机推荐
- java类中定义接口
今天看到一个java类中定义了接口,写个备忘录,记录一下 package com.gxf.test; public class Test_interface { public interface sh ...
- xml之基础了解
1.简介 1>什么XML语言(eXtensible Markup Language) 可扩展标记语言XML是SGML的子集,其目标是允许普通的SGML在Web上以目前HTML的方式被服务.接受和 ...
- 程序调试手段之gdb, vxworks shell
调试一个程序主要用到的功能: 启动程序 设置函数断点 设置数据断点 单步执行 查看内存值 修改内存值 linux下的gdb,和vxworks下的shell 虽然使用方式和调试命令略有不同,但是都能满足 ...
- 玩耍Hibernate系列(一)--基础知识
Hibernate框架介绍: Hibernate ORM 主要用于持久化对象(最常用的框架) Hibernate Search 用于对对象进行搜索,底层基于Apache Lucene做的 Hib ...
- Mac下的常用Shell命令
今天介绍一下在Mac的终端中一些常用的Shell命令: 1.查看当前工作目录的完整路径 pwd (pwd的原意是:print work directiory,而不是密码password的意思,呵呵) ...
- OS X 使用技巧——在Finder窗口标题栏上显示路径
Finder窗口默认显示当前文件夹的名称或当前所在的模式(例如AirDrop).如果想要显示路径(用User/[当前用户账号名称]/Documents 替代以前显示的Documents),打开终端并运 ...
- UML 用例图,时序图,活动图的定义以及区别
1.用例图,时序图,活动图的定义 1.用例图: 用例图描述了系统提供的一个功能单元.用例图的主要目的是帮助开发团队以一种可视化的方式理解系统的功能需求,包括基于基本流程的"角色" ...
- JVM 崩溃 Failed to write core dump解决办法 WINDOWS
JVM 崩溃 Failed to write core dump解决办法 WINDOWS MIT key words: JVM,崩溃,windows,Failed,core dump,虚拟内存 最近从 ...
- DOM中事件绑定补充方法
先将上一篇文章中提到的为元素增加事件的方法和移除事件的方法拿过来: <span style="font-size:18px;">//跨浏览器添加事件 function ...
- Dynamic Programming - Part2
实现如下: public static void main(String[] args) { String squence1 = "ABCBDAB"; String squence ...