search
Homephp教程php手册PHP Hash算法:Times33算法代码实例

PHP Hash算法:Times33算法代码实例

   这篇文章主要介绍了PHP Hash算法:Times33算法代码实例,本文直接给出实现代码,需要的朋友可以参考下

  最近看书,里面提到了一些Hash算法。比较有印象的是Times33,当时理解不是很透测,今天写了段程序来验证了一下。

  先上代码:

  复制代码 代码如下:

  

  /**

  * CRC32 Hash function

  * @param $str

  * @return int

  */

  function hash32($str)

  {

  return crc32($str) >> 16 & 0x7FFFFFFF;

  }

  /**

  * Times33 Hash function

  * @param $str

  * @return int

  */

  function hash33($str)

  {

  $hash = 0;

  for($i=0; $i

  $hash += 33 * $hash + ord($str{$i});

  }

  return $hash & 0x7FFFFFFF;

  }

  $n = 10;

  // Test Case 1

  $stat = array();

  for($i=0; $i

  $str = substr(md5(microtime(true)), 0, 8);

  $p = hash32($str) % $n;

  if(isset($stat[$p])){

  $stat[$p]++;

  }else{

  $stat[$p] = 1;

  }

  }

  print_r($stat);

  // Test Case 2

  $stat = array();

  for($i=0; $i

  $str = substr(md5(microtime(true)), 0, 8);

  $p = hash33($str) % $n;

  if(isset($stat[$p])){

  $stat[$p]++;

  }else{

  $stat[$p] = 1;

  }

  }

  print_r($stat);

  以上有两个测试用例。第一个,用CRC32的方法;第二个是Times33的算法实现。

  效果:

  结果分布,两种算法不相上下(估计是数据源的问题,md5只有0-f)。也有文章说CRC32的分布更均匀(参考链接:)

  但耗费时间,CRC32比Times33快将近一倍。

  为什么是33?

  即是素数(质数),也是奇数。除了33,还有131, 1313, 5381等。PHP内置的Hash函数用的是5381,在“鸟哥”的一篇博文中也有提到。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software