如何加速树的创建

时间:2015-06-23 14:46:16

标签: 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
                    {
                        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);
}

在这种情况下,我得到的数据格式为

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

并且有多个这样的股票。问题是这个过程需要花费很长时间才能完成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;
}

如果我能提供更多,请告诉我。并感谢您的帮助

1 个答案:

答案 0 :(得分:0)

好的,

所以我所做的是我在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;
}

此外,我已经检查了记录日志的功能,因此很少被调用的小功能(对于内部...)我已经删除了这些日志。对于900股票而言,这一切都将时间从4.30分钟缩短至约1.30分钟。但是我想问一下我能做些什么来加快速度。

还有一个问题: 对于从数据库(sqlite数据库)中提取数据的许多函数,只有1个函数,connection.Open()需要花费很多时间。这个问题可能是因为连接打开了很长时间?或者是否有可能另一个已经打开的连接,所以关闭它并开始这个连接需要时间?