2
0
mirror of https://github.com/boostorg/bloom.git synced 2026-01-19 04:02:11 +00:00

added fpr_c.cpp

This commit is contained in:
joaquintides
2025-04-04 10:30:04 +02:00
parent c2b92c2825
commit edcda9c487
3 changed files with 128 additions and 2 deletions

View File

@@ -5,6 +5,14 @@
#
# See http://www.boost.org/libs/bloom for library home page.
project : default-build release <link>static ;
import-search /boost/config/checks ;
exe comparison_table : comparison_table.cpp ;
import config : requires ;
project
: default-build release <link>static
: requirements [ requires cxx14_generic_lambdas ]
;
exe comparison_table : comparison_table.cpp ;
exe fpr_c : fpr_c.cpp ;

View File

@@ -63,6 +63,7 @@ void resume_timing()
#include <boost/mp11/list.hpp>
#include <boost/mp11/utility.hpp>
#include <boost/unordered/unordered_flat_set.hpp>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>

117
benchmark/fpr_c.cpp Normal file
View File

@@ -0,0 +1,117 @@
/* For a given filter type, outputs FPR vs. c = m/n with optimum k.
*
* Copyright 2025 Joaquin M Lopez Munoz.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See https://www.boost.org/libs/bloom for library home page.
*/
#include <boost/algorithm/string/replace.hpp>
#include <boost/bloom/filter.hpp>
#include <boost/bloom/block.hpp>
#include <boost/bloom/multiblock.hpp>
#include <boost/core/detail/splitmix64.hpp>
#include <boost/mp11.hpp>
#include <boost/type_index.hpp>
#include <boost/unordered/unordered_flat_set.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <vector>
template<typename Filter>
double fpr(std::size_t c)
{
using value_type=typename Filter::value_type;
std::size_t num_elements=(std::size_t)(1000/Filter::fpr_for(1,c));
std::vector<value_type> data_in,data_out;
{
boost::detail::splitmix64 rng;
boost::unordered_flat_set<value_type> unique;
for(std::size_t i=0;i<num_elements;++i){
for(;;){
auto x=value_type(rng());
if(unique.insert(x).second){
data_in.push_back(x);
break;
}
}
}
for(std::size_t i=0;i<num_elements;++i){
for(;;){
auto x=value_type(rng());
if(!unique.contains(x)){
data_out.push_back(x);
break;
}
}
}
}
double fpr=0.0;
{
std::size_t res=0;
Filter f(c*num_elements);
for(const auto& x:data_in)f.insert(x);
for(const auto& x:data_out)res+=f.may_contain(x);
fpr=(double)res/num_elements;
}
return fpr;
}
using namespace boost::bloom;
/* change this definition to test another filter type */
template<std::size_t K>
using filter=boost::bloom::filter<int,1,multiblock<std::uint32_t,K>,1>;
/* change this to your desired c range */
std::size_t c_min=4,
c_max=24;
/* you may need to change this if optimum k > k_max */
std::size_t k_max=20;
using fpr_function=std::function<double(std::size_t)>;
static std::vector<fpr_function> fprs=[]
{
std::vector<fpr_function> fprs;
using ks=boost::mp11::mp_iota_c<20,1>;
boost::mp11::mp_for_each<ks>([&](auto K){
fprs.emplace_back(fpr< ::filter<K> >);
});
return fprs;
}();
int main()
{
std::string filter_name=
boost::typeindex::type_id< ::filter<666> >().pretty_name();
boost::replace_all(filter_name,"boost::bloom::","");
boost::replace_all(filter_name,"class ","");
boost::replace_all(filter_name,"struct ","");
boost::replace_all(filter_name,"666","K");
std::cout
<<filter_name<<"\n"
<<"fpr;c;k\n";
std::size_t k=0;
for(std::size_t c=c_min;c<=c_max;++c){
double r=fprs[k](c);
for(;;){
if(k==k_max){
std::cerr<<"k_max hit, raise it and rerun\n";
return EXIT_FAILURE;
}
double rn=fprs[k+1](c);
if(rn>=r)break;
r=rn;
++k;
}
std::cout<<c<<";"<<r<<";"<<(k+1)<<"\n";
}
}