首页 > 代码库 > AutoCAD .net 开发 SelectionFilter Foreach Linq 性能比较
AutoCAD .net 开发 SelectionFilter Foreach Linq 性能比较
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Diagnostics;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.EditorInput;/// <summary>/// 选择集比较/// </summary>/// [assembly: CommandClass(typeof(TipsAndTricks.Class1))]namespace TipsAndTricks{ public class Class1 { [CommandMethod("Test")] public void test() { DoFilter(FilterByEdSelectionFilter); DoFilter(FilterByLinq); DoFilter(FilterByForeach); } private static void DoFilter(Action action) { var ed = Application.DocumentManager.MdiActiveDocument.Editor; var sw = Stopwatch.StartNew(); action(); sw.Stop(); ed.WriteMessage($"{sw.Elapsed.TotalMilliseconds}\n"); } public void FilterByEdSelectionFilter() { var ed = Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] circType = new TypedValue[1]; circType.SetValue(new TypedValue((int)DxfCode.Start, "CIRCLE"), 0); SelectionFilter filtCirc = new SelectionFilter(circType); PromptSelectionResult resPrompt; resPrompt = ed.SelectAll(filtCirc); if (resPrompt.Status == PromptStatus.OK) { SelectionSet circSelSet = resPrompt.Value; ed.WriteMessage($"ed count: {circSelSet.Count.ToString()}\n");//vs2015 新语法 可以正常编译.net 4.0的项目 } } public void FilterByLinq() { var db = HostApplicationServices.WorkingDatabase; var ed = Application.DocumentManager.MdiActiveDocument.Editor; using (var tr = db.TransactionManager.StartTransaction()) { var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; var mspace = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false) as BlockTableRecord; var circles = mspace.Cast<ObjectId>() .Where(id => id.ObjectClass.DxfName.Equals("CIRCLE")); //过滤条件很关键,对性能影响很大 ed.WriteMessage($"linq count: {circles.Count().ToString()}\n"); } } public void FilterByForeach() { var db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; using (var tr = db.TransactionManager.StartTransaction()) { var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; var mspace = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false) as BlockTableRecord; var circles = new List<ObjectId>(); foreach (ObjectId id in mspace) { var dbObj = tr.GetObject(id, OpenMode.ForRead) as Circle; if (dbObj != null) { circles.Add(dbObj.Id); } } ed.WriteMessage($"Foreach count: {circles.Count.ToString()}\n");
} } }}
目前看来 linq 效率挺高的,以后开发可以放心用linq做二次开发了.
------------------------
4m的dwg 图纸 测试结果:
ed count: 877
96.0967
linq count: 877
67.8107
Foreach count: 877
156.6604
AutoCAD .net 开发 SelectionFilter Foreach Linq 性能比较
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。