简体   繁体   中英

How to speed up tree creation

I have this code to generate a temp tree whose code is as follows

object regionSale = regionValue.GetValueAsString();

if (root.Children.Count > 0)
{
    if ((tmpNode.Data.Level) == (levelNested - 1))
    {
        var newChild = new Node
        {
            Data = new NodeData
                    {
                        Level = levelNested,
                        RegionName = elemNested.GetValueAsString(),
                        RegionValue = NAValue.Equals(regionSale.ToString())
                            ? null
                            : (double?)regionValue.GetValueAsFloat64()
                    },
            Parent = tmpNode
        };

        tmpNode.Children.Add(newChild);
        tmpNode = newChild;

    }
    else if (tmpNode.Data.Level == levelNested)
    {
        var node = tmpNode.Parent;
        var newChild = new Node
        {
            Data = new NodeData
                    {
                        Level = levelNested,
                        RegionName = elemNested.GetValueAsString(),
                        RegionValue = NAValue.Equals(regionSale.ToString())
                            ? null
                            : (double?)regionValue.GetValueAsFloat64()
                    },
            Parent = node
        };

        node.Children.Add(newChild);
        tmpNode = newChild;
    }
    else
    {
        var parentNode = tmpNode.Parent;
        while ((parentNode.Data.Level) != (levelNested - 1))
        {
            parentNode = parentNode.Parent;
        }
        var newChild = new Node
        {
            Data = new NodeData
                    {
                        Level = levelNested,
                        RegionName = elemNested.GetValueAsString(),
                        RegionValue = NAValue.Equals(regionSale.ToString())
                            ? null
                            : (double?)regionValue.GetValueAsFloat64()
                    },
            Parent = parentNode
        };

        parentNode.Children.Add(newChild);
        tmpNode = newChild;
    }
}
else
{
    var children = new Node();
    children.Data = new NodeData
    {
        Level = levelNested,
        RegionName = elemNested.GetValueAsString(),
        RegionValue = NAValue.Equals(regionSale.ToString())
            ? null
            : (double?)regionValue.GetValueAsFloat64()
    };

    children.Parent = root;
    root.Children.Add(children);
    tmpNode = children;
}

The data passed to this function is a root node like:

for (var nestedIndex = 0; nestedIndex < numofBulkValues; nestedIndex++)
{
    var bulkElementNested = refBulkField.GetValueAsElement(nestedIndex);

    var elemNested = bulkElementNested.GetElement(0);
    var levelElement = bulkElementNested.GetElement(1);
    var regionValue = bulkElementNested.GetElement(2);
    var levelNested = levelElement.GetValueAsInt32();

    tmpNode = GenerateTree(root, tmpNode, elemNested, regionValue, levelNested);
}

In this situation, the data i get is in the format

ADSK UW EQUITY  
Europe, Middle East and Africa  Level=1
The Americas  Level=1
   U.S  Level=2
   Other Americas  Level=2
The Asia/Pacific  Level=1
   Other Asia/Pacific  Level=2
   Japan  Level=2
Reconciliation  Level=1

and there are multiple such equities. the problem is that this process is taking a long time almost 9 seconds to do but only takes 16 seconds to display the actual result. Yes, this is the core of the application and very important so it cannot be skipped. Is there any way to reduce the time to create this tree?

my node class is as follows:

public class Node
{
    public Node()
    {

    }
    public Node(Node node)
        : this()
    {
        if (node == null)
            return;
        this.Data = new NodeData(node.Data);
        if (node.Children != null)
            this.Children = new List<Node>(node.Children);
        this.Parent = new Node(node.Parent);
    }
    public NodeData Data;
    public List<Node> Children = new List<Node>();
    public Node Parent;
}
public class NodeData
{
    public NodeData()
    {

    }
    public NodeData(NodeData nodeData)
        : this()
    {
        if (nodeData == null)
            return;
        this.RegionName = nodeData.RegionName;
        this.RegionValue = nodeData.RegionValue;
        this.Level = nodeData.Level;

    }
    public string RegionName;
    public double? RegionValue;
    public int Level;
}

If there is more i can provide please let me know. and thanks for any help

Okay,

So what i have done is that I have made changes in the Node.cs Class as follows:

public class Node
{
    public Node()
    {

    }
    public Node(Node node)
        : this()
    {
        if (node == null)
            return;
        if (node.Children != null)
            this.Children = new List<Node>(node.Children);
        this.Parent = new Node(node.Parent);
        this.RegionName = nodeData.RegionName;
        this.RegionValue = nodeData.RegionValue;
        this.Level = nodeData.Level;
    }
    public List<Node> Children = new List<Node>();
    public Node Parent;
    public string RegionName;
    public double? RegionValue;
    public int Level;
}

Also i have checked the functions where the log is being recorded, so small functions which get called a lot(for inside for..) i have removed those logs. This all has reduced the time from 4.30 minutes to about 1.30 minutes for 900 equities. But i wanted to ask if there is something more i can do to make it faster.

There is one other problem: Only for 1 function out of many which pulls the data from the database(sqlite database), connection.Open() takes a lot of time. Can this problem be because the connection is Open for a long time?? or is there a possibility of another connection which is already open,so to close that and start this connection takes time?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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