Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
240
Performance with UltraTree and adding Node the second time
posted

Hello,

I use Version 12.1 and have a problem with the performance when adding nodes. In my sample program I have the following code:

         this.ultraTree1.BeginUpdate();
         var watch = Stopwatch.StartNew();
         this.ultraTree1.Nodes.Clear();
         for(int i = 0; i < 5000; i++)
         {
            var node = new UltraTreeNode
            {
               Text = i.ToString()
            };
            ultraTree1.Nodes.Add(node);
            var b = node.Bounds; // this needs the time
         }
         watch.Stop();
         Debug.WriteLine(watch.ElapsedMilliseconds);
         this.ultraTree1.EndUpdate();

When I use it the first time the performace is ok. On my machine ElapsedMilliseconds ~ 400. The second time the ElapsedMilliseconds ~ 9000. Is their a solution to have a better performance when I use it the second time? In the real program I need the Bounds property to make some calculation how large is the necessary wiidth of the control to suppress the horizontal scroll bar.

Thanks Joachim

Parents
No Data
Reply
  • 469350
    Offline posted

    Hi Joachim,

    I don't think you can check the bounds of a node inside of the BeginUpdate/EndUpdate block like this. The Bounds are calculated when the node is painted. So either the call to the Bounds property is causing the tree to paint (which defeats the purpose of BeginUpdate), or else the results of the Bounds property will not be accurate inside this loop.

    So I think what you can do to make this code more efficient is to take the Bounds calculation out of the loop the delay it until afterward.This way the tree only have to paint once in order to calculate the bounds of every node. I changed the code like so and it's fast every time:


                this.ultraTree1.BeginUpdate();
                var watch = Stopwatch.StartNew();
                this.ultraTree1.Nodes.Clear();
                for (int i = 0; i < 5000; i++)
                {
                    var node = new UltraTreeNode
                    {
                        Text = i.ToString()
                    };
                    ultraTree1.Nodes.Add(node);
                    //var b = node.Bounds; // this needs the time
                }
                watch.Stop();
                Debug.WriteLine(watch.ElapsedMilliseconds);
                this.ultraTree1.EndUpdate();

                this.ultraTree1.Update();
                foreach (UltraTreeNode node in this.ultraTree1.Nodes)
                {
                    var b = node.Bounds; // this needs the time
                }

Children