关于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语言的人都能耳熟能详,也能谈得头头是道,但是,在实际运用中,当这些内容交织在一起时,大家却不一定能搞地清清楚楚,本文的目的正 ...
随机推荐
- OA Framework - How to Find the Correct Version of JDeveloper to Use with E-Business Suite 11i or Release 12.x (Doc ID 416708.1)
APPLIES TO: Oracle Applications Framework - Version 11.5.10.0 to 12.2.2 [Release 11.5.10 to 12.2] In ...
- STL之map
参见http://www.cplusplus.com/reference/map/map/ template < class Key, ...
- Team Homework #3 软件工程在北航——IloveSE
任务要求: 采访以前上过北航 (计算机系/软件学院) 软件工程课的同学.现在上研/工作的也可以. 采访问题如下:* 平均每周花在这门课上的时间 (包括上课/作业/上机) * 平均写的代码总行数 ...
- JavaScript 闭包详解
一.Javascript闭包的用途 事实上,通过使用闭包,我们可以做很多事情.比如模拟面向对象的代码风格:更优雅,更简洁的表达出代码:在某些方面提升代码的执行效率. 1.匿名自执行函数 我们知道所有的 ...
- 使用Java 8 Lambda表达式对Employee类进行操作
1,首先定义Employee类. package coffee.how.to.program.early.objects.chapter15; public class Employee { priv ...
- HDU 5564 Clarke and digits 状压dp+矩阵加速
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5564 题意: 求长度在[L,R]范围,并且能整除7的整数的总数. 题解: 考虑最原始的想法: dp[ ...
- ngcordova 监控网络制式改变
ngcordova 监控网络制式改变 keywords cordova,phonegap,ionic,network,网络制式 API参考 http://ngcordova.com/docs/plug ...
- Word2007插入两种页码
做毕设,摘要,Abstract,目录,第一章,现在想要“摘要,Abstract,目录”编页码“为罗马数字,第一章开始为阿拉伯数字,可以按如下步骤: 1.各部分插入分页符,这与插入两种页码无关,不过是为 ...
- SimpleDateFormat
范例: Date date = new Date();SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd,E,HH. ...
- 对话框Dialog
QMainWindow QMainWindow是 Qt 框架带来的一个预定义好的主窗口类. 主窗口,就是一个普通意义上的应用程序(不是指游戏之类的那种)最顶层的窗口.通常是由一个标题栏,一个菜单栏,若 ...