2013-03-02 76 views
2

我试图实现一个给定矩形和由用户决定的多个多边形的算法,它可以识别它们是在里面,外面还是相交矩形,并提供所述多边形。我编码算法,它的工作原理,但我注意到编译后,它至少需要20秒启动(如果我第二次,第三次或任何其他时间启动它,这不会发生)。调用一个函数会让我的代码变得很慢

试图弄清楚我的代码太慢了什么,我注意到如果我删除调用函数来确定多边形相对于矩形的位置,程序立即运行。

我试图找到一些错误,但没有发现任何

// struct used in the function 
struct Polygon 
{ 
    int ** points; 
    int vertices; 
}; 
// inside, outside and over are the number of polygons that are inside, outside or intersect the rectangle, 
// they're initialized to 0 in the main. 
// down_side, up_side are the y_coordinate of the two horizontals sides. 
// left_side, right_side are the x_coordinate of the two vertical sides. 
void checkPolygons(Polygon * polygon, int & inside, int & outside, int & over, unsigned int polygons, const unsigned int down_side, const unsigned int up_side, const unsigned int left_side, const unsigned int right_side) 
{ 
    for (unsigned int pol = 0; pol < polygons; ++pol) 
    { 
     unsigned int insideVertices = 0; 
     unsigned int vertices = polygon[ pol ].vertices; 

     for (unsigned int point = 0; point < vertices; ++point) 
     { 
      unsigned int x_coordinate = polygon[ pol ].points[ point ][ 0 ]; 
      unsigned int y_coordinate = polygon[ pol ].points[ point ][ 1 ]; 

      if ((x_coordinate <= right_side) and (x_coordinate >= left_side) and (y_coordinate <= up_side) and (y_coordinate >= down_side)) 
      { 
       insideVertices++; 
      } 

     } 

     if (insideVertices == 0) 
      ++outside; 
     else if (insideVertices == vertices) 
      ++inside; 
     else 
      ++over; 
    } 
} 
+0

你在用什么编译器/操作系统? – fritzone 2013-03-02 21:15:46

+0

在Windows下的MinGW(gcc) – doplumi 2013-03-02 21:17:55

+0

也许codereview.stackexchange.com? 'unsigned int x_coordinate = polygon [pol] .points [point] [0];'看起来像是杀手级的缓存。 – 2013-03-02 21:17:56

回答

2

检查你的抗病毒活性和配置。它可能正在扫描新编译的可执行文件中的病毒。如果是这种情况,您可能希望从病毒扫描中排除您编译的目录。

+1

优秀...当真实生活入侵抽象领域的程序员沉迷于 – fritzone 2013-03-02 21:50:30

+0

@fritzone发生过几次。 :) – 2013-03-02 22:15:34

相关问题