我对调试配置中的boost::geometry::intersection性能有疑问。我项目的一部分有很多(数百万)多边形 - 多边形类型的交叉点。与发布相比,它的调试速度非常慢。所以在这个“交叉点”部分之后,我需要等待很多时间来调试问题。在调试模式下我能做些什么来加快它的速度?
VS2010中简单Win32控制台项目代码示例:
#include "stdafx.h"
#include <time.h>
#include <deque>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/foreach.hpp>
bool get_poly_intersection_area_S_2d( const double *poly_x_1, const int poly_n_1, const double *poly_x_2, const int poly_n_2, double *S)
{
// intersects two 2d polygons using boost::geometry library
// polygons are in 3d format [(x0, y0, 0.0) (x1, y1, 0.0) .... (xn, yn, 0.0) ]
// S is resulting area of intersection
// returns true if intersection exists (area > DBL_EPSILON) and false otherwise
typedef boost::geometry::model::d2::point_xy<double> bg_point;
typedef boost::geometry::model::polygon< bg_point, false, false > bg_polygon;
*S = 0.0;
bg_polygon bg_poly_1, bg_poly_2;
// init boost 2d polygons by our double 3D polygons
for(int i=0; i<poly_n_1; i++)
bg_poly_1.outer().push_back(bg_point(poly_x_1[i*3], poly_x_1[i*3+1]));
for(int i=0; i<poly_n_2; i++)
bg_poly_2.outer().push_back(bg_point(poly_x_2[i*3], poly_x_2[i*3+1]));
// correct polygons
boost::geometry::correct(bg_poly_1);
boost::geometry::correct(bg_poly_2);
// call intersection
std::deque<bg_polygon> output;
bool res = boost::geometry::intersection(bg_poly_1, bg_poly_2, output);
if(!res)
return false;
// for each polygon of intersection we add area
BOOST_FOREACH(bg_polygon const& p, output)
{
double s = boost::geometry::area(p);
*S += s;
}
// no intersection
if(fabs(*S) <= DBL_EPSILON)
return false;
// intersection > DBL_EPSILON
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
double p1[4 * 3] = {0,0,0, 2,0,0, 2,2,0, 0,2,0};
double p2[4 * 3] = {1,1,0, 3,1,0, 3,3,0, 1,3,0};
clock_t start_t, finish_t;
start_t = clock();
for(int i=0;i<5000; i++)
{
double s;
for(int k=0; k<4; k++)
{
p1[k*4] += i;
p2[k*4] += i;
}
get_poly_intersection_area_S_2d(p1, 4, p2, 4, &s);
}
finish_t = clock();
printf("time=%.3f s\n", double(finish_t - start_t)/1000.);
return 0;
}
在 Debug 中需要 15 秒,在 Release 中需要 0.1 秒。而这里只有 5000 个多边形交叉点。对于数百万人来说,它会慢得多。