c++ - 调用函数使我的代码慢得离谱

标签 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;
    }
}

最佳答案

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

关于c++ - 调用函数使我的代码慢得离谱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15179504/

相关文章:

c++ - 未实现的 Tesseract 函数

oracle - 您如何获得 Oracle 中的最大可能日期?

c++ - 静态cpp中的非静态成员

c# - 访问 C# 字典中的元素

c++ - 如何使用 C++ 通过答案集检查答案?

javascript - JS脚本在CodeIgniter中的放置

从第二个表开始计数时 MySQL 查询速度问题

C++ 运算符重载输入运算符 >> 以类外的对象作为成员

c# - 我可以加快我的特殊回合功能吗?

javascript - jQuery 克隆形式增加名称、类、ID、数据 ID 等所有属性