最近看书,里面提到了一些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
}
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,在“鸟哥”的一篇博文中也有提到。

Methods to modify array elements in PHP include direct assignment and batch modification using functions. 1. For indexed arrays, such as $colors=['red','green','blue'], the second element can be modified by $colors[1]='yellow'. 2. For associative arrays, such as $person=['name'=>'John','age'=>30], the value of age can be modified by $person['age']=31. 3. Use array_map or array_walk functions to modify array elements in batches, such as $numbers=array_map(fun

Implementing hook functions in PHP can be implemented through observer mode or event-driven programming. The specific steps are as follows: 1. Create a HookManager class to register and trigger hooks. 2. Use the registerHook method to register the hook and trigger the hook by the triggerHook method when needed. Hook functions can improve the scalability and flexibility of the code, but pay attention to performance overhead and debugging complexity.

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

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
