如何最快地计算php中的设置位数?

时间:2013-05-31 02:36:38

标签: php algorithm set bit

我只是想在php中找到一些最快的设置位计数功能。

例如,0010101 => 3,00011110 => 4

我看到有很好的算法可以在c ++中实现。 How to count the number of set bits in a 32-bit integer?

是否有任何php内置函数或最快的用户自定义函数?

5 个答案:

答案 0 :(得分:7)

您可以尝试使用二进制AND应用掩码,并使用shift来逐个测试位,使用循环迭代32次。

function getBitCount($value) {

    $count = 0;
    while($value)
    {
        $count += ($value & 1);
        $value = $value >> 1;
    }

    return $count;
}

您还可以轻松地将您的功能转换为PHP风格

function NumberOfSetBits($v)
{
    $c = $v - (($v >> 1) & 0x55555555);
    $c = (($c >> 2) & 0x33333333) + ($c & 0x33333333);
    $c = (($c >> 4) + $c) & 0x0F0F0F0F;
    $c = (($c >> 8) + $c) & 0x00FF00FF;
    $c = (($c >> 16) + $c) & 0x0000FFFF;
    return $c;
}

答案 1 :(得分:2)

我的基准代码

start_benchmark();
for ($i = 0; $i < 1000000; $i++) {
    getBitCount($i);
}
end_benchmark();

start_benchmark();
for ($i = 0; $i < 1000000; $i++) {
    NumberOfSetBits($i);
}
end_benchmark();
start_benchmark();
for ($i = 0; $i < 1000000; $i++) {
    substr_count(decbin($i), '1');
}
end_benchmark();

基准测试结果:

benchmark(NumberOfSetBits()):1.429042 milleseconds

benchmark(substr_count()):1.672635 milleseconds

benchmark(getBitCount()):10.464981 milleseconds

我认为NumberOfSetBits()和substr_count()是最好的。 感谢。

答案 2 :(得分:2)

此选项比NumberOfSetBits($ v)快一点

function bitsCount(int $integer)
{
    $count = $integer - (($integer >> 1) & 0x55555555);
    $count = (($count >> 2) & 0x33333333) + ($count & 0x33333333);
    $count = ((((($count >> 4) + $count) & 0x0F0F0F0F) * 0x01010101) >> 24) & 0xFF;

    return $count;
}

答案 3 :(得分:1)

我可以找出一些方法,但不确定哪一种方法最快:

  • 使用substr_count()
  • 用''替换所有无'1'字符,然后使用strlen()
  • 使用preg_match_all()

PS:如果你从一个整数开始,这些例子将首先使用decbin()。

答案 4 :(得分:1)

还有很多其他方法;但对于十进制32位整数,NumberOfSetBits肯定是最快的。

我最近偶然发现了Brian Kernighan´s algorithmO(log(n))而不是其他大多数O(n)。我不知道为什么它在这里看起来不那么快;但它仍然具有超过所有其他非专业功能的可测量优势。

当然,NumberOfSetBits无法击败O(1)

我的基准:

function getBitCount($value) { $count = 0; while($value) { $count += ($value & 1); $value = $value >> 1; } return $count; }

function getBitCount2($value) { $count = 0; while($value) { if ($value & 1)$count++; $value >>= 1; } return $count; }
// if() instead of +=; >>=1 instead of assignment: sometimes slower, sometimes faster
function getBitCount2a($value) { for($count = 0;$value;$value >>= 1) if($value & 1)$count ++; return $count; }
// for instead of while: sometimes slower, sometimes faster

function getBitCount3($value) { for($i=1,$count=0;$i;$i<<=1) if($value&$i)$count++; return $count; }
// shifting the mask: incredibly slow (always shifts all bits)
function getBitCount3a($value) { for($i=1,$count=0;$i;$i<<=1) !($value&$i) ?: $count++; return $count; }
// with ternary instead of if: even slower

function NumberOfSetBits($v) {
// longest (in source code bytes), but fastest
    $c = $v - (($v >> 1) & 0x55555555); $c = (($c >> 2) & 0x33333333) + ($c & 0x33333333);
    $c = (($c >> 4) + $c) & 0x0F0F0F0F; $c = (($c >> 8) + $c) & 0x00FF00FF;
    $c = (($c >> 16) + $c) & 0x0000FFFF;    return $c;
}

function bitsByPregReplace($n) { return strlen(preg_replace('_0_','',decbin($n))); }
function bitsByNegPregReplace($n) { return strlen(preg_replace('/[^1]/','',decbin($n))); }
function bitsByPregMatchAll($n) { return preg_match_all('/1/',decbin($n)); }

function bitsBySubstr($i) { return substr_count(decbin($i), '1'); }
function bitsBySubstrInt($i) { return substr_count(decbin($i), 1); }
// shortest (in source code bytes)

function bitsByCountChars($n){ return count_chars(decbin($n))[49]; }
// slowest by far
function bitsByCountChars1($n) { return count_chars(decbin($n),1)[49]; }
// throws a notice for $n=0

function Kernighan($n) { for(;$n;$c++)$n&=$n-1;return$c; }
// Brian Kernighan’s Algorithm

function benchmark($function)
{
    gc_collect_cycles();
    $t0=microtime();
    for($i=1e6;$i--;) $function($i);
    $t1=microtime();
    $t0=explode(' ', $t0); $t1=explode(' ', $t1);
    echo ($t1[0]-$t0[0])+($t1[1]-$t0[1]), " s\t$function\n";
}

benchmark('getBitCount');
benchmark('getBitCount2');
benchmark('getBitCount2a');
benchmark('getBitCount3');
benchmark('getBitCount3a');
benchmark('NumberOfSetBits');
benchmark('bitsBySubstr');
benchmark('bitsBySubstrInt');
benchmark('bitsByPregReplace');
benchmark('bitsByPregMatchAll');
benchmark('bitsByCountChars');
benchmark('bitsByCountChars1');
benchmark('decbin');

banchmark结果(已排序)

> php count-bits.php
 2.286831 s     decbin

 1.364934 s     NumberOfSetBits
 3.241821 s     Kernighan

 3.498779 s     bitsBySubstr*
 3.582412 s     getBitCount2a
 3.614841 s     getBitCount2
 3.751102 s     getBitCount
 3.769621 s     bitsBySubstrInt*

 5.806785 s     bitsByPregMatchAll*
 5.748319 s     bitsByCountChars1*
 6.350801 s     bitsByNegPregReplace*
 6.615289 s     bitsByPregReplace*

13.863838 s     getBitCount3
16.39626 s      getBitCount3a
19.304038 s     bitsByCountChars*

这些是我的一次运行中的数字(使用PHP 7.0.22);其他人在3.5秒组内表现出不同的顺序。我可以这么说 - 在我的机器上 - 这五个中的四个非常相等,由于类型转换,bitsBySubstrInt总是慢一点。

大多数其他方式都需要一个decbin(大多数需要比实际计数更长的时间;我在基准测试结果中用*标记它们);只有BitsBySubstr才能在没有那条腿的情况下接近胜利者。

我发现通过将其限制为仅存在的字符,可以使count_chars快3倍。似乎数组索引需要相当长的时间。

编辑:

  • 添加了另一个preg_replace版本
  • 已修复preg_match_all版本
  • 添加了Kernighan的算法(任意大小整数的最快算法)
  • 将垃圾收集添加到基准测试功能
  • reran benchmarkmarks
相关问题