`

PHP中Array的hash函数实现

阅读更多

今天回顾学习了PHP中变量实现的方法,在浏览其源码是发现在PHP中所有的数据类型通过一个union存储。

php语言是弱类型语言,其实现中通过记录变量的类型和值来实现其管理。

 

PHP中使用最多的非Array莫属了,那Array是如何实现的?

在PHP内部Array通过一个hashtable来实现,其中使用链接法解决hash冲突的问题,这样最坏情况下,查找Array元素的复杂度为O(N),最好则为1.

 

而其计算字符串hash值的方法如下,将源码摘出来以供查备:

ps:对于以下函数,仍有两点不明:

1.  hash = 5381设置的理由?

2.  这种step=8的循环方式是为了效率么?

 

static inline ulong zend_inline_hash_func(const char *arKey, uint nKeyLength)
{
    register ulong hash = 5381;                                                   //此处初始值的设置有什么玄机么?

    /* variant with the hash unrolled eight times */
    for (; nKeyLength >= 8; nKeyLength -= 8) {                         //这种step=8的方式是为何?
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;                         //比直接*33要快
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
    }   
    switch (nKeyLength) {
        case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */                             //此处是将剩余的字符hash
        case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */                     
        case 1: hash = ((hash << 5) + hash) + *arKey++; break;
        case 0: break;
EMPTY_SWITCH_DEFAULT_CASE()
    }   
    return hash;                                                                //返回hash值
}

 

1
2
分享到:
评论

相关推荐

    PHP函数速查效率手册 source code

    本书共分21章,PHP函数所涉及的范围包括MySQL服务器、Apache服务器、ODBC、String字符串、Array数组、Variable函数、时间日期、Hash、正则表达式、Session、Math、Filesystem、Directories、Mail、HTTP、URL、FTP、...

    PHP的array_diff()函数在处理大数组时的效率问题

    php /** * 解决 php 5.2.6 以上版本 array_diff() 函数在处理 * 大数组时的需要花费超长时间的问题 * * 整理:http://www.CodeBit.cn * 来源:http://bugs.php.net/47643 */ function array_diff_fast($data1, $data...

    php 实现Hash表功能实例详解

    映射函数叫做Hash函数,存放记录的数组称为Hash表。 Hash函数把任意长度的和类型的key转换成固定长度输出。不同的key可能拥有相同的hash。 Hash表的时间复杂度为O(1) &lt;?php class HashTable{ private $arr = ...

    PHP中用hash实现的数组

    PHP中使用最多的非Array莫属了,那Array是如何实现的?在PHP内部Array通过一个hashtable来实现,其中使用链接法解决hash冲突的问题,这样最坏情况下,查找Array元素的复杂度为O(N),最好则为1. 而其计算字符串hash值...

    PHP5 完整官方 中文教程

    Arrays — Array 数组函数 Aspell — Aspell 函数(已废弃) BBCode — BBCode Functions BC math — BC math 高精度数学函数 bcompiler — BCompiler PHP 字节码编译器 Bzip2 — Bzip2 压缩函数 Calendar — ...

    PHP5中文参考手册

    Arrays — Array 数组函数 Aspell — Aspell 函数(已废弃) BBCode — BBCode Functions BC math — BC math 高精度数学函数 bcompiler — BCompiler PHP 字节码编译器 Bzip2 — Bzip2 压缩函数 Calendar — ...

    PHP手册2007整合中文版

    PHP,即“PHP: Hypertext Preprocessor”,是一种被广泛使用的开放源代码多用途脚本语言,尤其适用于 web 开发并可以嵌入到 HTML 中去。其语法利用了 C,Java 和 Perl,非常容易学习。该语言的主要目标是让 web 开发...

    PHP官方手册中文版

    Array 数组函数 VI. Aspell 函数(已废弃) VII. BBCode Functions VIII. BC math 高精度数学函数 IX. BCompiler PHP 字节码编译器 X. Bzip2 压缩函数 XI. Calendar 日历函数 XII. CCVS API 函数(已废弃) ...

    经纬度地址编码geohash.zip

    这个php扩展,提供了三个函数:/**  * $latitude //纬度  * $longitude //经度  * $precision //精密度, 默认是12  * 返回 $precision 长度的 string  */  geohash_encode($latitude, $longitude...

    PHP框架Gaeaphp.zip

    [$option=array()]]) 获取db实例Core::memcached([$key='default',[$servers=array(),[$options=array()]]]) 获取memcached实例项目函数库一些项目的全局函数位于/src/libs/functions/global.php,该文件会被自动加载...

    hashdigest:PHP 哈希生成器和验证器

    这是 PHP 函数 HASH 的包装器,用于使用 sha256 算法生成散列摘要,并根据给定数据验证给定散列。 安装 要求 PHP v&gt;=5.1.2+ 与作曲家 安装 Carbon 的最简单方法是通过 composer。 创建以下composer.json文件并运行 ...

    DX1.5 手机正式版

    ‘formhash’ =&gt; ”,//跟提交表单有关的HASH数据 ‘timestamp’ =&gt; TIMESTAMP,//程序执行时的时间戳 ’starttime’ =&gt; dmicrotime(),//程序开始执行的时间 ‘clientip’ =&gt; $this-&gt;_get_client_ip(),//用户IP ...

    MDPHP(麦迪php框架系统)

    功能函数库(Practic.php)中包含若干实用功能: I() 获取用户输入数据 U() 生成URL地址 C() 读取配置项 F() 写文件 M() 实例化一个模型 S() 缓存功能 P() 执行插件 N() 数据统计 hook() 钩子监听 cookie() cookie操作...

    PHP 加密 Password Hashing API基础知识点

    PHP 5.5 之后引入 Password hashing API 用于创建和校验哈希密码,它属于内核自带,无需进行任何扩展安装和配置。...1、password_hash(string password, int algo [, array options]) 使用足够强度的单向散列算法生成密

    X-Cart Gold 4.5.4.zip

    [* 2012年10月04日,ABR - 改进(0125385):优化的func_query_hash()函数。新增的测试。 [*] 2012年9月28日,ABR - 改进(0126569):改进的JavaScript代码缩进。 [*] 2012年9月28日,ABR - 改进(0126474):...

    ZendFramework中文文档

    9.5.3. Create dates from an array 9.6. Constants for General Date Functions 9.6.1. Using Constants 9.6.2. List of All Constants 9.6.3. Self-Defined OUTPUT Formats with ISO 9.6.4. Self-defined ...

Global site tag (gtag.js) - Google Analytics