调用函数会使我的代码变得非常慢

时间:2013-03-02 21:13:22

标签: c++ performance function call

我正在尝试实现一种给定矩形和用户决定的多个多边形的算法,可以识别它们是在矩形的内部,外部还是相交,并提供所述多边形的数量。

我编写了一个算法并且它可以工作,但是我注意到在编译之后它至少需要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;
    }
}

1 个答案:

答案 0 :(得分:2)

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