Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
boost::geometry::distance performance overhead compared to a straightforward implementation
#include <iostream> #include <iomanip> #include <ctime> #include <boost/geometry.hpp> #include <boost/geometry/geometries/point_xy.hpp> using namespace boost::geometry; using namespace std; const int N = 3000; struct Point { float x, y; float distance(const Point& p) const {return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));} }; array<model::d2::point_xy<float>, N> a0; array<Point, N> a1; float f0() { float d = 0; for (auto p: a0) for (auto q: a0) d += boost::geometry::distance(p, q); return d; } float f1() { float d = 0; for (auto p: a1) for (auto q: a1) d += p.distance(q); return d; } int main() { for (int i = 0; i < N; ++i) { a0[i] = {(float)i, (float)i}; a1[i] = {(float)i, (float)i}; } auto benchmark = [](float (*f)()) { const int N = 10; float result = 0; vector<double> timings(N); for (int i = 0; i < N; ++i) { volatile auto t0 = clock(); volatile auto r = f(); volatile auto t1 = clock(); result += r; timings[i] = (double)(t1-t0)/CLOCKS_PER_SEC; } sort(timings.begin(), timings.end()); cout << fixed << setprecision(6); cout << timings.front() << "s <ctime> min\n"; cout << timings[timings.size()/2] << "s <ctime> median\n"; cout << "Result: " << result << "\n\n"; return timings.front(); }; double overhead = benchmark(f0)/benchmark(f1); cout << "Boost.geometry overhead: " << setprecision(2) << overhead << "\n"; return 0; }
run
|
edit
|
history
|
help
0
Unpacking tuple
INHERIT_CTORS default constructor
Dynamically allocated array in unique_ptr with custom deleter.
Recursive Function Calling Example with Stack Addresses
Tilted uniform distribution random number generator over min/max range
function pointer
Test titlu
HTML Timetable generator.cpp
Template Meta Programming
Расстановка восьми ферзей на шахматной доске так, чтобы ни один не угрожал другому - C++