-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhyperloglog.php
151 lines (125 loc) · 3.5 KB
/
hyperloglog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @author Vadim Semenov <[email protected]>
* @date 10/12/12
* @time 1:25 PM
* @description HyperLogLog algorithm implementation
* @url http://algo.inria.fr/flajolet/Publications/FlFuGaMe07.pdfs
* @license MIT, see LICENSE-MIT.md
*/
function generateWords($count) {
$result = array();
while ($count > 0) {
$word = '';
for ($j = 0; $j < rand(1, 8); $j++) { // from 1char to 8chars
$word .= chr(rand(97, 122)); // a-z
}
for ($i = 0; $i < rand(1, 100); $i++) {
$result[] = $word;
$count--;
}
}
return $result;
}
function cardinality($arr) {
$arr = array_count_values($arr);
return count($arr);
}
class HyperLogLog {
const HASH_LENGTH = 32; // bites
const HASH_K = 5; // HASH_LENGTH = 2 ^ HASH_K
const ALPHA = 0.697122946; // 1 / (32 * integral(0,inf)( (log2(1+1/(1+x)))^32 dx))
/**
* Jenkins hash function
*
* @url http://en.wikipedia.org/wiki/Jenkins_hash_function
*
* @static
* @param $str
* @return int Hash
*/
private static function hash($str) {
$hash = 0;
for ($i = 0, $l = strlen($str); $i < $l; $i++) {
$hash += ord($str[$i]);
$hash += $hash << 10;
$hash ^= $hash >> 6;
}
$hash += $hash << 3;
$hash ^= $hash >> 6;
$hash += $hash << 16;
return $hash;
}
/**
* Offset of first 1-bit
*
* @example 00010 => 4
*
* @static
* @param int $bites
* @return int
*/
private static function scan1($bites) {
if ($bites == 0) {
return self::HASH_LENGTH - self::HASH_K;
}
$offset = floor(log($bites, 2));
$offset = self::HASH_LENGTH - self::HASH_K - $offset;
return $offset;
}
/**
* @static
*
* @param string $bites
* @param int $start >=1
* @param int $end <= HASH_LENGTH
*
* @return int slice of $bites
*/
private static function getBites($bites, $start, $end) {
$r = $bites >> (self::HASH_LENGTH - $end);
$r = $r & (pow(2, $end - $start + 1) - 1);
return $r;
}
/**
* @static
* @param $arr
* @return int Number of unique items in $arr
*/
public static function count($arr) {
$M = array();
foreach ($arr as $v) {
$h = self::hash($v);
$j = self::getBites($h, 1, self::HASH_K) + 1;
$w = self::getBites($h, self::HASH_K + 1, self::HASH_LENGTH);
$w = self::scan1($w);
if (!isset($M[$j]) || $M[$j] < $w) {
$M[$j] = $w;
}
}
$Z = 0;
for ($i = 1; $i <= self::HASH_LENGTH; $i++) {
if (isset($M[$i]) && $M[$i] != 0) {
$Z += 1 / pow(2, $M[$i]);
} else {
$Z += 1;
}
}
$Z = self::ALPHA * self::HASH_LENGTH * self::HASH_LENGTH / $Z;
return floor($Z);
}
}
$words = generateWords(10000);
echo "Number of words\n" . count($words) . "\n";
echo "------\nPrecision\n";
$s = microtime(1);
$m = memory_get_usage(1);
echo cardinality($words) . "\n";
echo 'time: ' . (microtime(1) - $s) . " sec\n";
echo 'mem: ' . (memory_get_usage(1) - $m) . "\n";
echo "------\nLogLog\n";
$s = microtime(1);
$m = memory_get_usage(1);
echo HyperLogLog::count($words) . "\n";
echo 'time: ' . (microtime(1) - $s) . " sec\n";
echo 'mem: ' . (memory_get_usage(1) - $m) . "\n";