简体   繁体   English

如何加快树的创建

[英]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. 问题在于此过程需要很长时间,几乎要花9秒钟,而显示实际结果只需要16秒钟。 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: 因此,我要做的是对Node.cs类进行了如下更改:

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. 所有这一切将900只股票的时间从4.30分钟减少到大约1.30分钟。 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. 还有一个问题:只有一个函数从数据库(sqlite数据库)中提取数据,connection.Open()需要大量时间。 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? 或者是否有可能已经打开了另一个连接,因此关闭该连接并启动该连接需要花费时间?

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

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