-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from outbrain/cms
More primitive, much faster counter
- Loading branch information
Showing
8 changed files
with
49 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
from collections import Counter | ||
|
||
|
||
class PrimitiveConstrainedCounter: | ||
""" | ||
A memory-efficient implementation of the count min sketch algorithm with optimized hashing using Numba JIT. | ||
""" | ||
|
||
def __init__(self, bound: int=(10**4) * 3): | ||
self.max_bound_thr = bound | ||
self.default_counter: Counter = Counter() | ||
|
||
def batch_add(self, lst): | ||
if len(self.default_counter) < self.max_bound_thr: | ||
self.default_counter = self.default_counter + Counter(lst) | ||
|
||
def add(self, val): | ||
if len(self.default_counter) < self.max_bound_thr: | ||
self.default_counter[val] += 1 | ||
|
||
|
||
if __name__ == '__main__': | ||
from collections import Counter | ||
|
||
depth = 8 | ||
width = 2**22 | ||
import numpy as np | ||
cms = PrimitiveConstrainedCounter() | ||
|
||
items = [1, 1, 2, 3, 3, 3, 4, 5, 2] * 10000 | ||
cms.batch_add(items) # Use the batch_add function | ||
|
||
print(Counter(items)) # Print the exact counts for comparison |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters