glibc | strlen

本文深入探讨了处理以null结尾的字符串的优化方法,包括如何快速扫描至null字符,确保字节对齐,以及实现高效算法的关键步骤。重点介绍了在不同场景下优化字符串操作的实用技巧。

 

brief:返回以 null 结尾的字符串的长度。通过一次性测试 4(sizeof(long int)) 字节来快速扫描至 null 字符。(Return the length of the null-terminated string STR. Scan for
the null terminator quickly by testing four bytes at a time.)

这样的优化的前提是字节对齐的保证:

Handle the first few characters by reading one character at a time.
Do this until CHAR_PTR is aligned on a longword boundary.

  /* 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;  /* 判断是否能 sizeof(longword) 被整除。能->到达对齐的边界了。
       ++char_ptr)
    if (*char_ptr == '\0')  /* null terminate */
      return char_ptr - str;

待写……

接下来:magic_bits,the real magic

转载于:https://www.cnblogs.com/excavator/p/4851707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值