关于“DevExpress.XtraTreeList.v10.2” 的树控件treelist和虚拟树IVirtualTreeListData运行速度的比较:
首先编写继承虚拟树接口IVirtualTreeListData的类,MyData如下:
namespace test_virtualTreeListAsZMX
{
//我的数据
public class MyData:TreeList.IVirtualTreeListData
{
protected MyData parentCore;
protected ArrayList chrildrenCore = new ArrayList();
protected object[] cellsCore;
public MyData(MyData parentCoreIn,object[] cellsCoreIn)
{
parentCore = parentCoreIn;
cellsCore = cellsCoreIn;
if (this.parentCore != null)
{
this.parentCore.chrildrenCore.Add(this);
}
}
public void VirtualTreeGetCellValue(VirtualTreeGetCellValueInfo info)
{
info.CellData = this.cellsCore[info.Column.AbsoluteIndex];
}
public void VirtualTreeGetChildNodes(VirtualTreeGetChildNodesInfo info)
{
info.Children = this.chrildrenCore;
}
public void VirtualTreeSetCellValue(VirtualTreeSetCellValueInfo info)
{
cellsCore[info.Column.AbsoluteIndex] = info.NewCellData;
}
}
}
其次编写main函数:
namespace test_virtualTreeListAsZMX
{
public partial class Form1 : Form
{
Stopwatch sw = new Stopwatch();
private int intMax=0;
public Form1()
{
InitializeComponent();
Initdata();
}
private void Initdata()
{
int maxNum=1000000;
MyData dataResource = new MyData(null,null);
MyData root1 = new MyData(dataResource,new string[] {"root1","high"});
MyData a1 = new MyData(root1,new string[] {"a1","middle"});
MyData a2=new MyData(a1,new string[] {"a2","low"});
MyData root2 = new MyData(dataResource, new string[] { "root2", "high" });
//MyData mydataTest = treelistData(root2);
TreeListColumn c1 = new TreeListColumn();
c1.Caption = "name";
c1.VisibleIndex = 0;
TreeListColumn c2 = new TreeListColumn();
c2.Caption = "value";
c2.VisibleIndex = 1;
this.treeList1.Columns.AddRange(new TreeListColumn[] {c1,c2});
this.treeList1.DataSource=dataResource;
//测试
//ordinaryTreeList
sw.Start();
TreeListColumn c5 = new TreeListColumn();
c5.Caption = "name";
c5.VisibleIndex = 0;
TreeListColumn c6 = new TreeListColumn();
c6.Caption = "value";
c6.VisibleIndex = 1;
this.ordinaryTreeList.Columns.AddRange(new TreeListColumn[] { c5, c6 });
TreeListNode root = this.ordinaryTreeList.AppendNode(new string[] { "a1", "low" }, 0);
root.Expanded = true;
for (int jj = 0; jj < maxNum; jj++)
{
TreeListNode tln2 = ordinaryTreeList.AppendNode(new string[] { "a2", "low" }, root);
}
ordinaryTreeList.ExpandAll();
sw.Stop();
Console.WriteLine("ordinaryTreeList_open:" + sw.ElapsedMilliseconds);
//virtualTreeList
sw.Start();
TreeListColumn c3 = new TreeListColumn();
c3.Caption = "name";
c3.VisibleIndex = 0;
TreeListColumn c4 = new TreeListColumn();
c4.Caption = "value";
c4.VisibleIndex = 1;
this.virtualTreeList.Columns.AddRange(new TreeListColumn[] { c3, c4 });
MyData dataResource1 = new MyData(null, null);
a2 = new MyData(dataResource1, new string[] { "a1", "low" });
for (int ii = 0; ii < maxNum; ii++)
{
MyData a3 = new MyData(a2, new string[] { "a2", "low" });
}
this.virtualTreeList.DataSource = dataResource1;
virtualTreeList.ExpandAll();
sw.Stop();
Console.WriteLine("virtualTreeList_open:" + sw.ElapsedMilliseconds);
}
private MyData treelistData(MyData myDataIn)
{
intMax++;
MyData imydata1 = new MyData(myDataIn, new string[] { "a2", "low" });
MyData imydata2=null;
if (intMax < 10)
{
imydata2 = treelistData(imydata1);
return imydata2;
}
else
return imydata1;
}
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}
运行结果如下:
当节点数为100时,差别不大:
普通树完全展开花费:243 millisecond。
虚拟树完全展开花费:256 millisecond。
节点数达到1000000个后,差别明显:
普通树完全展开花费:3106 millisecond。
虚拟树完全展开花费:7067 millisecond。
截图如下:
由此可见,虚拟树的好处还有待探索,希望能在其他方面发现它运行速度上的优势。