AutoCAD. NET development Selectionfilter Foreach LINQ Performance Comparison

Source: Internet
Author: User

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>///Selection Set comparison///</        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.MdiActiveDoc Ument.            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 new syntax can compile. NET 4.0 Project}}            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")); Filtration conditions are critical and have a large impact on performance ed. Writemessage ($ "LINQ count: {circles. Count ().            ToString ()}\n "); }} public void Filterbyforeach () {var db = Hostapplicationservices.workingdataba            Se            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 ");
} } }}

At present, LINQ is very efficient, and later development can be assured with LINQ two times development.

------------------------

DWG drawing test results for 4m:

Ed count:877
96.0967
LINQ count:877
67.8107
Foreach count:877
156.6604

AutoCAD. NET development Selectionfilter Foreach LINQ Performance Comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.