Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AI and Deep Learning by (50.2k points)

I'm trying to set up a tree (eventually for use in a "neural network" and attempting to make just the setup as efficient as possible. Unfortunately, even setting up the tree is taking approximately 3 minutes and I can't figure out what about it is making it so inefficient. I attempted to use pointers wherever possible to minimize the load, but it still is taking forever. What am I doing wrong?

PS. This is eventually for a Tic Tac Toe AI (yes I know it can be solved just by looking at the stupid game, but I wanted to do it as a simple AI to teach myself how.

Each branch of the tree would have 9 nodes, with each node branching out to make another 9. That gives the last set of branches approximately 400 million nodes. Is there ANY way to do this code more efficiently?

#include <iostream>

#include <vector>

using namespace std;

class Node;

class Set;


 

class Node {

    public:

        Node(double, Set*);

        Node();

        double value;

        Set * nextSet;

};

class Set {

    public:

        Set(vector<Node *>);

        Set();

        vector<Node *> nodes;

};

class NeuralNet {

    public:

        Set * firstSet;

};

Node::Node(double val, Set * newSet){

    value = val;

    nextSet = newSet;

}

Set::Set(vector<Node *> input){

    nodes = input;

}

Node::Node(){

    Set temp;

    nextSet = &temp;

}

Set::Set(){

    vector<Node *> temp;

    nodes = temp;

}

void setUpNeuralNetRecursive(Set * curSet, int curDepth){

    if(curDepth<9){

        for(int i=0;i<9;i++){

            Set newSet;

            Node newNode(1,&newSet);

            (*curSet).nodes.push_back(&newNode);

            setUpNeuralNetRecursive(&newSet, curDepth+1);

        }

    }

}

void setUpNeuralNet(NeuralNet net){

    Set newSet;

    net.firstSet=&newSet;

    setUpNeuralNetRecursive(&newSet, 0);

}

int main()

{

    cout << "Setting up neural network.  This may take up to 3 minutes." << endl;

    NeuralNet net;

    setUpNeuralNet(net);

    cout << "Setup ended." << endl;

    return 0;

}

1 Answer

0 votes
by (108k points)

In my opinion, the efficiency issues should not be left to the compiler, they should always be fixed in the c++ code. 

If you did something wrong that makes a program slower, fix it for sure. That includes messing up memory allocations and unnecessary copies. 

Assuming the code is correct, though, trying to optimize code should be left alone until you find a code block that underperforms during your stress testing. I wrote real time code for a long, long time and the worst thing you can do to yourself is optimize where it isn't needed.. the code tends to get weird and full of 'do it this way because it ran faster' commentary and the programmer falls behind refactoring working code that was fine instead of writing new code. When you need speed, there is a time and place to mess with it, just choose your battles carefully.

Here is a link which tells you about Efficient C/C++ Coding Techniques: http://www.open-std.org/jtc1/sc22/wg21/docs/ESC_Boston_01_304_paper.pdf

Browse Categories

...