开发者

c# .net iterating twice a list in a inner and outer loop - local copy or reference?

Hallo everyone,

i have a list of nodes ListNode and i want to draw a line between two nodes if there is an edge / link between them. My approach so far is:

public void drawGraphInBIM(ref BIM bim)
{
    foreach (Node nodeOuter in ListNode)
    {
        foreach (Node nodeInner in ListNode)
        {
            if (areNodesLinked(nodeOuter, nodeInner))
            {
                bim.drawPolygon(nodeOuter.XYZ, nodeInner.XYZ);
            }
        }
    }
}

I am wandering how the if there is a local开发者_如何学Python copy of ListNode for each loop or is there just a reference and nodeOuter and nodeInner are operating on the same ListNode?

Is there a better approach to this problem?

Cheers,

Dawit


It's the same ListNode. No local copies are made of reference types.


It is the same ListNode collection. You are not copying it.

The reason you're seeing this slow down is because you're current algorithm is O(N^2). It's going to do N^2 iterations for N items, so as your collection grows, the algorithm slows down exponentially.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜