简体   繁体   English

C++ Object 创建时间

[英]C++ Object Creation Time

So I'm building an A* search sort of thing and creating objects as below when needed within my algorithm.所以我正在构建一个 A* 搜索类型的东西,并在我的算法中需要时创建如下对象。 Thing is, they're taking.1 of a second each being created.问题是,它们每一个都需要 1 秒的时间来创建。 My search takes 40 seconds, 39 seconds of which is object creation.我的搜索需要 40 秒,其中 39 秒是 object 创建的。

I really got no clue.我真的一点头绪都没有。 Pretty new.很新。 Any help appreciated.任何帮助表示赞赏。

class Node{
private:
    int numAncestors;
    float gvalue;
    float hvalue;
    float fvalue;
    int adj;
    Node* parent;
public:
    inline Node(int vertex, int goal, vector< vector<double> > xy){

        adj = vertex - 1;
        float x1 = (float)xy[vertex-1][0];
        float y1 = (float)xy[vertex-1][1];
        float x2 = (float)xy[goal-1][0];
        float y2 = (float)xy[goal-1][1];
        hvalue = sqrtf((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
        //fvalue = hvalue + gvalue;
    }
    inline float getF(){
        return hvalue + gvalue;
    }

    inline void setG(float newG){
        gvalue = newG;
    }
    inline float getG(){
        return gvalue;
    }

    inline int getAncestors(){

        return numAncestors;
    }

    void setAncestors(int ancestors){

        numAncestors = ancestors;
    }
    void setParent(Node* n, vector< vector<double> > xy){

        parent = n;
        float x1 = (float)xy[n->getAdj()][0];
        float y1 = (float)xy[n->getAdj()][1];
        float x2 = (float)xy[adj][0];
        float y2 = (float)xy[adj][1];
        float x = sqrtf((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));

        setG(n->getG() + x);
        setAncestors(n->getAncestors()+1);
     }

    inline Node* getParent(){

        return parent;
    }

    inline int getAdj(){

        return adj;
    }
};

This takes.1 seconds:这需要.1 秒:

clock_t nodetest = clock();    
Node* s = new Node(g,e,xy);
printf("Time taken: %.2fs\n", (double)(clock() - nodetest)/CLOCKS_PER_SEC);

This takes 0.0 seconds:这需要 0.0 秒:

clock_t thisLoop = clock();
float x1 = (float)xy[x-1][0];
float y1 = (float)xy[x-1][1];
float x2 = (float)xy[current->getAdj()][0];
float y2 = (float)xy[current->getAdj()][1];
float x = sqrtf((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
printf("Time taken: %.2fs\n", (double)(clock() - thisLoop)/CLOCKS_PER_SEC);

I would have thought maybe it was what the object was up to or whatever, but it seems from that the entirety of the time spent is just in object creation.我原以为这可能是 object 的目的或其他原因,但似乎所有花费的时间都只是在 object 的创建中。

When you call the Node constructor, you are passing xy by value , meaning a copy is made of the entire array.当您调用Node构造函数时,您通过 value传递xy ,这意味着由整个数组制作副本。 You should pass it by const reference :您应该通过const reference传递它:

Node(int vertex, int goal, const vector<vector<double>> &xy)

This should also be done for setParent .这也应该为setParent完成。

Other notes:其他注意事项:

The Node constructor does not initialize all of its members. Node构造函数不会初始化它的所有成员。 In particular, parent will have a garbage value in it, which can lead to strange bugs later.特别是parent里面会有一个垃圾值,这可能会导致以后出现奇怪的错误。

The Node constructor and setParent share a bunch of code that does some work. Node构造函数和setParent共享一堆可以工作的代码。 This should be placed in a (private) member function that can be called to avoid the code duplication.这应该放在可以调用以避免代码重复的(私有)成员 function 中。

The various get functions can be const , eg float getF() const;各种get函数可以是const ,例如float getF() const; . .

You don't need the inline keyword, since any function defined within the class definition is implicitly inline.您不需要inline关键字,因为在 class 定义中定义的任何 function 都是隐式内联的。

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

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