C# 如何加速树的创建

C# 如何加速树的创建,c#,performance,tree,C#,Performance,Tree,我有这个代码来生成一个临时树,其代码如下 object regionSale = regionValue.GetValueAsString(); if (root.Children.Count > 0) { if ((tmpNode.Data.Level) == (levelNested - 1)) { var newChild = new Node { Data = new NodeData

我有这个代码来生成一个临时树,其代码如下

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;
}
传递到此函数的数据是根节点,如:

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);
}
这类股票有多种。问题是,这个过程需要很长时间,几乎9秒才能完成,但显示实际结果只需要16秒。是的,这是应用程序的核心,非常重要,因此不能跳过。有没有办法缩短创建此树的时间

我的节点类如下所示:

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;
}
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;
}
公共类节点
{
公共节点()
{
}
公共节点(节点)
:此()
{
if(node==null)
返回;
this.Data=新节点数据(node.Data);
if(node.Children!=null)
this.Children=新列表(node.Children);
this.Parent=新节点(Node.Parent);
}
公共节点数据;
public List Children=new List();
公共节点父节点;
}
公共类节点数据
{
公共NodeData()
{
}
公共NodeData(NodeData NodeData)
:此()
{
如果(nodeData==null)
返回;
this.RegionName=nodeData.RegionName;
this.RegionValue=nodeData.RegionValue;
this.Level=nodeData.Level;
}
公共字符串RegionName;
公共双区域价值;
公共智力水平;
}
如果有更多我可以提供,请让我知道。谢谢你的帮助好的

因此,我所做的是对Node.cs类进行了如下更改:

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;
}
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;
}
公共类节点
{
公共节点()
{
}
公共节点(节点)
:此()
{
if(node==null)
返回;
if(node.Children!=null)
this.Children=新列表(node.Children);
this.Parent=新节点(Node.Parent);
this.RegionName=nodeData.RegionName;
this.RegionValue=nodeData.RegionValue;
this.Level=nodeData.Level;
}
public List Children=new List();
公共节点父节点;
公共字符串RegionName;
公共双区域价值;
公共智力水平;
}
此外,我还检查了记录日志的函数,因此经常调用的小函数(对于inside for…)我已经删除了这些日志。所有这些都将900只股票的交易时间从4.30分钟缩短到1.30分钟左右。但我想问一下,我是否还能做些什么来加快速度

还有一个问题: 在从数据库(sqlite数据库)提取数据的众多函数中,只有一个函数需要connection.Open()花费大量时间。此问题是否可能是因为连接已打开很长时间??或者是否存在另一个已打开的连接的可能性,因此关闭该连接并启动此连接需要时间?

好的

因此,我所做的是对Node.cs类进行了如下更改:

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;
}
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;
}
公共类节点
{
公共节点()
{
}
公共节点(节点)
:此()
{
if(node==null)
返回;
if(node.Children!=null)
this.Children=新列表(node.Children);
this.Parent=新节点(Node.Parent);
this.RegionName=nodeData.RegionName;
this.RegionValue=nodeData.RegionValue;
this.Level=nodeData.Level;
}
public List Children=new List();
公共节点父节点;
公共字符串RegionName;
公共双区域价值;
公共智力水平;
}
此外,我还检查了记录日志的函数,因此经常调用的小函数(对于inside for…)我已经删除了这些日志。所有这些都将900只股票的交易时间从4.30分钟缩短到1.30分钟左右。但我想问一下,我是否还能做些什么来加快速度

还有一个问题:
在从数据库(sqlite数据库)提取数据的众多函数中,只有一个函数需要connection.Open()花费大量时间。此问题是否可能是因为连接已打开很长时间??或者是否存在另一个已打开的连接的可能性,因此关闭该连接并启动此连接需要时间?

探查器将帮助您缩小一点上下文范围(例如!)GetValueAsString()需要100毫秒,这样您就知道必须查找的位置。乍一看(没有分析),我看到了许多列表操作(和(重新)分配)。如果列表可能的大小或多或少是已知的…您可以从那里开始(创建具有合理初始容量的列表以防止重新分配)。@AdrianoRepetti嗨,我已经检查了探查器,它说GetValueAsString使用GetValueAsString只是一个示例!计时器?您已经有探查器结果要检查…@AdrianoRepetti而且列表大小根本不知道,因为即使对于相同的股票,它可能会从一次执行到下一次执行。可以添加或删除许多这样的区域。@AdrianoRepetti yes。。但我正在检查数据拉取是问题还是树生成的问题。。探查器并没有为这个函数显示太多内容。探查器将帮助您缩小一点上下文范围(例如!)GetValueAsString()需要100毫秒,那么您就知道要在哪里查找。乍一看(没有分析),我看到了许多列表操作(和(重新)分配)。如果列表可能的大小或多或少是已知的…您可以从那里开始(创建具有合理初始容量的列表以防止重新分配)。@AdrianoRepetti嗨,我已经检查了探查器,它说GetValueAsString使用GetValueAsString只是一个示例!计时器?您已经有探查器结果要检查…@AdrianoRepetti而且列表大小根本不知道,因为即使对于相同的股票,它可能会从一次执行到下一次执行。可以添加或删除许多这样的区域。@AdrianoRepetti yes。。但我正在检查数据拉取是问题还是树生成的问题。。探查器没有显示太多的功能。杰伊,我不想太迂腐,但…想知道是什么