2013-10-28 109 views
0

我有個問題boost :: geometry :: intersection性能調試配置。我的項目的一部分有很多(百萬)多邊形 - 多邊形類型的交點。與發佈相比,它的調試速度非常慢。所以我需要等很多時間來調試這個「交集」部分之後的問題。我能做些什麼來加速它在調試模式?簡單的Win32控制檯項目的在VS2010Boost :: geometry ::在調試模式下的交集性能

代碼例如:

#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; 
} 

在調試它需要15秒,在Release0.1秒。而這裏只有5000個多邊形交叉點。對於數百萬人來說,它會慢得多。

回答

0

Boost.Geometry的速度很大程度上受stl(vector,deque,map)速度的影響,這在調試模式下相當慢。

您可能會嘗試解決該問題,例如此鏈接:

Why does my STL code run so slowly when I have the debugger/IDE attached?

Why is this code 100 times slower in debug?

尤其是第二個鏈接顯示設置_HAS_ITERATOR_DEBUGGING 0可能有很大的幫助,因爲迭代器內部嚴重Boost.Geometry

或者你可以嘗試使用使用stl端口。

+0

非常感謝,Barend!設置_HAS_ITERATOR_DEBUGGING = 0和_NO_DEBUG_HEAP = 1在10次以上加速示例。所以現在,不是15秒,而是1.5秒。但是另一個問題發生。使用_HAS_ITERATOR_DEBUGGING = 0鏈接不同的庫並且沒有導致鏈接錯誤LNK2038:爲'_ITERATOR_DEBUG_LEVEL'檢測到不匹配:值'2'與值'0'不匹配。 – alxand

+0

好,它有幫助。我沒有這方面的經驗,但鏈接也提到了這一點,並提供了一個可能的解決方案:如果你有這些其他庫的來源,你可以使用相同的定義來編譯它們。 –

0

最好的方法是找出一種方法來重現使用小數據集調試的問題。這有很多幫助,特別是在您解決問題時創建迴歸測試時。

你寫的:「調試問題這種‘交集’部分之後」

這個建議,我認爲你應該選擇保存從交叉部分的輸出,因此,你需要運行一次,然後可以選擇加載保存的部分結果,然後在調試模式下從該處運行程序的其餘部分。

+0

嗨,ravenspoint!我也考慮過將交集結果保存在發佈模式中,然後在調試中加載。這不是很方便,但如果我不能找到另一個可接受的方式解決這個問題,我會這樣做。謝謝! – alxand

相關問題