繁体   English   中英

长时间调用 C++ 中的 function [暂停]

[英]Taking long for calling the function in C++ [on hold]

我在 C++ 中写了一段代码,其中一个函数在主 function 中调用时花费了太多时间; 问题在于 function 调用作为执行主 function 中的行的时间和执行 function 中的代码的时间是巨大的。 因此,我们将不胜感激有关此事的任何建议。

{
  Function(){
   t1 = clock();
   .
   .
   .
   .
   .
   .
   t2 = clock();

  }

  main(){
  t3 = clock();
  Function();
  t4 = clock();
 }
}

t4-t3 >>>> t2-t1(时间差很大)。

编辑:我很抱歉发布不完整的问题,首先,我得到大约 0.5s 的一些小输入的差异,即 ((t4-t3)-(t2-t1) ~ 0.5s),第二个,不发布代码的原因是它有点大,我没想到有人通过代码 go,我希望有人遇到类似问题,无论如何代码在下面,

//this function tries divides the neighbourhood points of a point 'pt'
//into two categories, empty (or list_pts_empty, are the ones with zero
//potential value which can be obtained from map_potential) and non-zero ones
//(list_pts_bound, which are the ones with a non-zero potential value which
//can also be obtained from map_potential and list_val_bound is the corresponding 
//potential values of the list_pts_bound).

//I'M SURE THE FUNCTION CAN BE WRITTEN IN SIMPLER WAYS BUT THE ONE I
//NEED IS MORE GENERIC AND COULD ABLE TO MODIFIED FOR VARIOUS TYPES OF
//NEIGHBOURHOODS AROUND A POINT.

    void getNeighPts(std::pair<float,float> pt,Eigen::MatrixXf potentials_map,std::vector<std::pair<int,int>>& list_pts_bound,std::vector<float>& list_vals_bound,std::vector<std::pair<int,int>>& list_pts_empty){

        //map_potential contains potential values for various points.

        ct0 = clock();

        list_pts_empty.clear();
        list_vals_bound.clear();
        list_pts_bound.clear();

        //clearing the vectors, incase if they have any elements already.

        std::pair<int,int> ind;
        float pot_val_ind;

        std::vector<std::pair<int,int>> neighs_perp;
        neighs_perp.push_back(std::make_pair(0,1));
        neighs_perp.push_back(std::make_pair(0,-1));
        neighs_perp.push_back(std::make_pair(1,0));
        neighs_perp.push_back(std::make_pair(-1,0));

        int i,j;

        for(int k = 0;k < neighs_perp.size();k++)
        {
            i = neighs_perp[k].first;
            j = neighs_perp[k].second;

            ind = std::make_pair(pt.first+i,pt.second+j);
            if(ind.first >= 0 && ind.second >= 0 && ind.first < potentials_map.cols() && ind.second < potentials_map.rows())
            {
                pot_val_ind = potentials_map(ind.second,ind.first);
                if(pot_val_ind > 0)
                {
                    list_pts_bound.push_back(ind);
                    list_vals_bound.push_back(potentials_map(ind.second,ind.first) + 1);
                }

                else if(inRo(pot_val_ind) == 0)
                {
                    list_pts_empty.push_back(ind);
                }
            }

        }
        ct1 = clock();

}

this function runs in a loop in main function and when I add up all the time differences inside the function(ie, ct1-ct0) it is 0.5s less than what it is calculated in the main function (as it shown in the 1st code )。

如果有人对给定的数据仍然不满意,我可以通过 email 分享整个代码。

提前致谢:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM