Merge pull request #6289 from afabri/Hash_map-benchmark-GF

This commit is contained in:
Laurent Rineau 2022-04-01 10:17:40 +02:00 committed by GitHub
commit ae28ae8a31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 108 additions and 101 deletions

View File

@ -2,12 +2,10 @@
#include <fstream> #include <fstream>
#include <map> #include <map>
#include <algorithm> #include <algorithm>
#include <random>
#include <CGAL/Simple_cartesian.h> #include <CGAL/Simple_cartesian.h>
#include <CGAL/Polyhedron_3.h> #include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/boost/graph/graph_traits_Polyhedron_3.h>
#include <CGAL/boost/graph/properties_Polyhedron_3.h>
#include <CGAL/Unique_hash_map.h> #include <CGAL/Unique_hash_map.h>
#include <CGAL/Timer.h> #include <CGAL/Timer.h>
@ -18,18 +16,104 @@
#include <boost/random/random_number_generator.hpp> #include <boost/random/random_number_generator.hpp>
#include <boost/random/linear_congruential.hpp> #include <boost/random/linear_congruential.hpp>
typedef CGAL::Simple_cartesian<double> Kernel; typedef CGAL::Simple_cartesian<int> Kernel;
typedef Kernel::Point_3 Point_3; struct Point_3 : Kernel::Point_3 {
using Kernel::Point_3::operator=;
Point_3() : Kernel::Point_3(0,0,0) {}
Point_3(int i) : Kernel::Point_3(i,0,0) {}
};
typedef Kernel::Vector_3 Vector_3; typedef Kernel::Vector_3 Vector_3;
typedef CGAL::Polyhedron_3<Kernel> Mesh; typedef CGAL::Polyhedron_3<Kernel> Mesh;
typedef boost::property_map<Mesh,CGAL::vertex_point_t>::type VPM;
typedef boost::graph_traits<Mesh>::vertex_descriptor vertex_descriptor; typedef boost::graph_traits<Mesh>::vertex_descriptor vertex_descriptor;
typedef std::vector<vertex_descriptor> Vertex_list;
typedef CGAL::Timer Timer; typedef CGAL::Timer Timer;
template <typename Map>
auto reserve(Map& sm, typename Map::size_type ii) -> decltype(sm.reserve(ii), void()){
sm.reserve(ii);
}
template <typename Map>
Point_3 lookup(Map& sm,vertex_descriptor vh){
auto search = sm.find(vh);
if(search != sm.end())
return search->second;
return Point_3();
}
template <typename X, typename Y>
Point_3 lookup(CGAL::Unique_hash_map<X,Y>& sm,vertex_descriptor vh){
const CGAL::Unique_hash_map<X,Y>& const_sm = sm;
return const_sm[vh];
}
template <typename Map>
int fct(int ii, int jj, const Vertex_list& V, const Vertex_list& V2, const VPM& vpm, const std::string& s)
{
int x = 0;
Timer construct, query, lookups;
construct.start();
for(int j=0; j <jj; j++){
Map sm;
reserve(sm,ii);
for(vertex_descriptor vh : V){
sm[vh] = get(vpm,vh);
}
}
construct.stop();
Map sm;
for(vertex_descriptor vh : V){
sm[vh] = get(vpm,vh);
}
query.start();
for(int j=0; j <jj; j++){
for(vertex_descriptor vh : V){
x+= sm[vh].x();
}
}
query.stop();
int y=0;
lookups.start();
for(int j=0; j <jj; j++){
for(vertex_descriptor vh : V2){
y+= lookup(sm,vh).x();
}
}
lookups.stop();
if(y != 0) { std::cout << y << " != 0" << std::endl;}
std::cerr << s << construct.time() << " sec.\t| " << query.time() << " sec.\t| " << lookups.time() << " sec." << std::endl;
return x;
}
void random_mesh(int ii, int jj,Mesh& mesh,VPM& vpm,Vertex_list& V)
{
for(int i =0; i < ii; i++){
vertex_descriptor vd = add_vertex(mesh);
put(vpm,vd, Point_3(i));
}
for(vertex_descriptor vd : vertices(mesh)){
V.push_back(vd);
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(V.begin(), V.end(), g);
}
void fct(int ii, int jj) void fct(int ii, int jj)
{ {
typedef std::map<vertex_descriptor,Point_3> SM; typedef std::map<vertex_descriptor,Point_3> SM;
@ -37,106 +121,28 @@ void fct(int ii, int jj)
typedef boost::unordered_map<vertex_descriptor,Point_3> BUM; typedef boost::unordered_map<vertex_descriptor,Point_3> BUM;
typedef CGAL::Unique_hash_map<vertex_descriptor, Point_3> UHM; typedef CGAL::Unique_hash_map<vertex_descriptor, Point_3> UHM;
Mesh mesh1;
VPM vpm1 = get(CGAL::vertex_point,mesh1);
Vertex_list V1;
random_mesh(ii,jj,mesh1,vpm1,V1);
Mesh mesh; Mesh mesh2;
typedef boost::property_map<Mesh,CGAL::vertex_point_t>::type VPM; VPM vpm2 = get(CGAL::vertex_point,mesh2);
VPM vpm = get(CGAL::vertex_point,mesh); Vertex_list V2;
random_mesh(ii,jj,mesh2,vpm2,V2);
Vector_3 v(0,0,0); std::cerr << std::endl << ii << " items and queries (repeated " << jj << " times)" << std::endl;
std::cerr << "Name\t\t\t| Version\t| Construction\t| Queries\t| Lookups" << std::endl;
for(int i =0; i < ii; i++){
vertex_descriptor vd = add_vertex(mesh);
put(vpm,vd, Point_3(i,0,0));
}
std::vector<vertex_descriptor> V;
for(vertex_descriptor vd : vertices(mesh)){
V.push_back(vd);
}
random_shuffle(V.begin(), V.end());
Timer tsmc, tsumc, tbumc, tuhmc;
Timer tsmq, tsumq, tbumq, tuhmq;
for(int j=0; j <jj; j++){
{
tsmc.start();
SM sm;
for(vertex_descriptor vh : V){
sm[vh] = get(vpm,vh);
}
tsmc.stop();
tsmq.start();
for(vertex_descriptor vh : V){
v = v + (sm[vh] - CGAL::ORIGIN);
}
tsmq.stop();
}
{
tsumc.start();
SUM sm;
for(vertex_descriptor vh : V){
sm[vh] = get(vpm,vh);
}
tsumc.stop();
tsumq.start();
for(vertex_descriptor vh : V){
v = v + (sm[vh] - CGAL::ORIGIN);
}
tsumq.stop();
}
{
tbumc.start();
BUM sm;
for(vertex_descriptor vh : V){
sm[vh] = get(vpm,vh);
}
tbumc.stop();
tbumq.start();
for(vertex_descriptor vh : V){
v = v + (sm[vh] - CGAL::ORIGIN);
}
tbumq.stop();
}
{
tuhmc.start();
UHM sm;
for(vertex_descriptor vh : V){
sm[vh] = get(vpm,vh);
}
tuhmc.stop();
tuhmq.start();
for(vertex_descriptor vh : V){
v = v + (sm[vh] - CGAL::ORIGIN);
}
tuhmq.stop();
}
}
std::cerr << ii << " items and queries (repeated " << jj << " times)" << std::endl;
std::cerr << "std::map construction : "<< tsmc.time() << " sec." << std::endl;
std::cerr << "std::map queries : "<< tsmq.time() << " sec." << std::endl;
std::cerr << "std::unordered_map construction : "<< tsumc.time() << " sec." << std::endl;
std::cerr << "std::unordered_map queries : "<< tsumq.time() << " sec." << std::endl;
std::cerr << "boost::unordered_map construction : "<< tbumc.time() << " sec." << std::endl;
std::cerr << "boost::unordered_map queries : "<< tbumq.time() << " sec.\n" << std::endl;
std::cerr << "Unique_hash_map construction : "<< tuhmc.time() << " sec." << std::endl;
std::cerr << "Unique_hash_map queries : "<< tuhmq.time() << " sec.\n" << std::endl;
int temp;
int res = fct<SM>(ii,jj, V1,V2, vpm1, "std::map\t\t|\t\t| " );
temp = fct<SUM>(ii,jj,V1,V2, vpm1, "std::unordered_map\t|\t\t| " );
if(temp != res){ std::cout << temp << " != " << res << std::endl;}
temp = fct<BUM>(ii,jj, V1,V2, vpm1, "boost::unordered_map\t|\t\t| " );
if(temp != res){ std::cout << temp << " != " << res << std::endl;}
temp = fct<UHM>(ii,jj,V1,V2, vpm1, "CGAL::Unique_hash_map\t| master\t| " );
if(temp != res){ std::cout << temp << " != " << res << std::endl;}
} }
int main(int , char* argv[]) int main(int , char* argv[])
@ -148,5 +154,6 @@ int main(int , char* argv[])
fct(500, 20000); fct(500, 20000);
fct(250, 40000); fct(250, 40000);
fct(100, 100000); fct(100, 100000);
fct(10, 1000000);
return 0; return 0;
} }