File tree 1 file changed +10
-9
lines changed
1 file changed +10
-9
lines changed Original file line number Diff line number Diff line change @@ -70,27 +70,28 @@ def random_string(size: int) -> str:
70
70
return "" .join (choices (ascii_lowercase + " " , k = size ))
71
71
72
72
73
- def test_probability (bits : int = 64 , n : int = 20 ) -> None :
74
- b = Bloom (size = bits )
73
+ def test_probability (filter_bits : int = 64 , added_elements : int = 20 ) -> None :
74
+ b = Bloom (size = filter_bits )
75
75
76
76
k = len (b .HASH_FUNCTIONS )
77
- estimated_error_rate_beforehand = (1 - (1 - 1 / bits ) ** (k * n )) ** k
77
+ estimated_error_rate_beforehand = (
78
+ 1 - (1 - 1 / filter_bits ) ** (k * added_elements )
79
+ ) ** k
78
80
79
- added = {random_string (10 ) for i in range (n )}
80
- for a in added :
81
- b .add (a )
81
+ not_added = {random_string (10 ) for i in range (1000 )}
82
+ for _ in range ( added_elements ) :
83
+ b .add (not_added . pop () )
82
84
83
85
n_ones = bin (b .bitstring ).count ("1" )
84
- estimated_error_rate = (n_ones / bits ) ** k
86
+ estimated_error_rate = (n_ones / filter_bits ) ** k
85
87
86
- not_added = {random_string (10 ) for i in range (1000 )}
87
88
errors = 0
88
89
for string in not_added :
89
90
if b .exists (string ):
90
91
errors += 1
91
92
error_rate = errors / len (not_added )
92
93
93
- print (f"total = { len (not_added )} , errors = { errors } , error_rate = { error_rate } " )
94
+ print (f"error_rate = { errors } / { len (not_added )} = { error_rate } " )
94
95
print (f"{ estimated_error_rate = } " )
95
96
print (f"{ estimated_error_rate_beforehand = } " )
96
97
You can’t perform that action at this time.
0 commit comments