<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Slow ResultBuffer in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8401354#M24305</link>
    <description>&lt;P&gt;I made some tests and it looks like it is the Resultbuffer.Add() method which is very slow.&lt;/P&gt;
&lt;P&gt;You should use the overload ResultBuffer(params TypedValue[])&amp;nbsp; constructor instead.&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("BENCH")]
        public static void Bench()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            BenchArray(ed);
            BenchAdd(ed);
        }

        public static void BenchArray(Editor ed)
        {
            var sw = new Stopwatch();
            sw.Start();
            var tvs = new TypedValue[50000];
            for (int i = 0; i &amp;lt; 50000; i++)
            {
                tvs[i] = new TypedValue((int)DxfCode.Int32, i);
            }
            ResultBuffer resbuf = new ResultBuffer(tvs);
            sw.Stop();
            ed.WriteMessage($"\nElapsed milliseconds to build from a TypedValues array: {sw.ElapsedMilliseconds}");
        }

        public static void BenchAdd(Editor ed)
        {
            var sw = new Stopwatch();
            sw.Start();
            var resbuf = new ResultBuffer();
            for (int i = 0; i &amp;lt; 50000; i++)
            {
                resbuf.Add(new TypedValue((int)DxfCode.Int32, i));
            }
            sw.Stop();
            ed.WriteMessage($"\nElapsed milliseconds to add 50000 TypedValues: {sw.ElapsedMilliseconds}");
        }&lt;/PRE&gt;
&lt;P&gt;Gives:&lt;/P&gt;
&lt;PRE&gt;Elapsed milliseconds to build from a TypedValues array: 18
Elapsed milliseconds to add 50000 TypedValues: 3734&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Nov 2018 14:04:08 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2018-11-14T14:04:08Z</dc:date>
    <item>
      <title>Slow ResultBuffer</title>
      <link>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8400456#M24302</link>
      <description>&lt;P&gt;I want to save a lot of data in DWG via XRecord but when making the ResultBuffer for it, it is very slow.&lt;/P&gt;&lt;P&gt;Here is a sample code for the ResultBuffer:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        public static void SaveSurfaceDataToDictionary(string dictName)
        {
            ResultBuffer resultBuffer = new ResultBuffer();
            foreach (long objectHandle in Lisp.plineHandleList) //List length = 59260
            {
                resultBuffer.Add(new TypedValue((int)DxfCode.Handle, objectHandle));
            }
            SetXrecord(dictName, "ObjectHandleList", resultBuffer);

            resultBuffer = new ResultBuffer();
            foreach (TriangleNet.Geometry.Vertex v in Lisp.polygonList.Points) //List length = 22766
            {
                resultBuffer.Add(new TypedValue((int)DxfCode.UcsOrg, new Point3d(v.X, v.Y, v.Z)));
            }
            SetXrecord(dictName, "PolygonPointsList", resultBuffer);

            resultBuffer = new ResultBuffer();
            List&amp;lt;ISegment&amp;gt; segmentList = Lisp.polygonList.Segments;
            foreach (Segment segment in segmentList) //List length = 17022
            {
                TriangleNet.Geometry.Vertex v;
                v = segment.GetVertex(0);
                resultBuffer.Add(new TypedValue((int)DxfCode.UcsOrg, new Point3d(v.X, v.Y, v.Z)));
                v = segment.GetVertex(1);
                resultBuffer.Add(new TypedValue((int)DxfCode.UcsOrg, new Point3d(v.X, v.Y, v.Z)));
            }
            SetXrecord(dictName, "PolygonSegmentsList", resultBuffer);
        }&lt;/PRE&gt;&lt;PRE&gt;        public static void SetXrecord(string dictName, string key, ResultBuffer resbuf)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary NOD =
                    (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                DBDictionary dict;
                if (NOD.Contains(dictName))
                {
                    dict = (DBDictionary)tr.GetObject(NOD.GetAt(dictName), OpenMode.ForWrite);
                }
                else
                {
                    dict = new DBDictionary();
                    NOD.UpgradeOpen();
                    NOD.SetAt(dictName, dict);
                    tr.AddNewlyCreatedDBObject(dict, true);
                }
                Xrecord xRec = new Xrecord();
                xRec.Data = resbuf;
                dict.SetAt(key, xRec);
                tr.AddNewlyCreatedDBObject(xRec, true);
                tr.Commit();
            }
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The ResultBuffer operation takes 12 seconds. Way to slow.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea or alternative for this?&lt;/P&gt;&lt;P&gt;I want to keep my data in DWG database.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 06:17:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8400456#M24302</guid>
      <dc:creator>office</dc:creator>
      <dc:date>2018-11-14T06:17:53Z</dc:date>
    </item>
    <item>
      <title>Re: Slow ResultBuffer</title>
      <link>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8400524#M24303</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Except the fact you could start the transaction and get or or create the dictionary only once in the SaveSurfaceDataToDictionary() method ans pass them to the SetXrecord() method I do not see anything unefficient in your code structure.&lt;/P&gt;
&lt;P&gt;But we do not know anything about the custom objects you use (Lisp, TriangleNet.Geometry.Vertex, Segment). The implementation of these types may be the cause of the slow down.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 07:04:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8400524#M24303</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-11-14T07:04:14Z</dc:date>
    </item>
    <item>
      <title>Re: Slow ResultBuffer</title>
      <link>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8400546#M24304</link>
      <description>&lt;P&gt;The custom objects are alright.&lt;/P&gt;&lt;P&gt;The problem is the resultbuffer.&lt;/P&gt;&lt;P&gt;Here is a sample code which takes 21 seconds without any implementations:&lt;/P&gt;&lt;PRE&gt;        public static void SaveSurfaceDataToDictionary(string dictName)
        {
            ResultBuffer resultBuffer = new ResultBuffer();
            for (int i = 0; i &amp;lt; 50000; i++)
            {
                resultBuffer.Add(new TypedValue((int)DxfCode.Int32, i));
            }

            resultBuffer = new ResultBuffer();
            for (int i = 0; i &amp;lt; 50000; i++)
            {
                resultBuffer.Add(new TypedValue((int)DxfCode.Int32, i));
            }

            resultBuffer = new ResultBuffer();
            for (int i = 0; i &amp;lt; 50000; i++)
            {
                resultBuffer.Add(new TypedValue((int)DxfCode.Int32, i));
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Nov 2018 07:19:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8400546#M24304</guid>
      <dc:creator>office</dc:creator>
      <dc:date>2018-11-14T07:19:38Z</dc:date>
    </item>
    <item>
      <title>Re: Slow ResultBuffer</title>
      <link>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8401354#M24305</link>
      <description>&lt;P&gt;I made some tests and it looks like it is the Resultbuffer.Add() method which is very slow.&lt;/P&gt;
&lt;P&gt;You should use the overload ResultBuffer(params TypedValue[])&amp;nbsp; constructor instead.&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("BENCH")]
        public static void Bench()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            BenchArray(ed);
            BenchAdd(ed);
        }

        public static void BenchArray(Editor ed)
        {
            var sw = new Stopwatch();
            sw.Start();
            var tvs = new TypedValue[50000];
            for (int i = 0; i &amp;lt; 50000; i++)
            {
                tvs[i] = new TypedValue((int)DxfCode.Int32, i);
            }
            ResultBuffer resbuf = new ResultBuffer(tvs);
            sw.Stop();
            ed.WriteMessage($"\nElapsed milliseconds to build from a TypedValues array: {sw.ElapsedMilliseconds}");
        }

        public static void BenchAdd(Editor ed)
        {
            var sw = new Stopwatch();
            sw.Start();
            var resbuf = new ResultBuffer();
            for (int i = 0; i &amp;lt; 50000; i++)
            {
                resbuf.Add(new TypedValue((int)DxfCode.Int32, i));
            }
            sw.Stop();
            ed.WriteMessage($"\nElapsed milliseconds to add 50000 TypedValues: {sw.ElapsedMilliseconds}");
        }&lt;/PRE&gt;
&lt;P&gt;Gives:&lt;/P&gt;
&lt;PRE&gt;Elapsed milliseconds to build from a TypedValues array: 18
Elapsed milliseconds to add 50000 TypedValues: 3734&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 14:04:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8401354#M24305</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-11-14T14:04:08Z</dc:date>
    </item>
    <item>
      <title>Re: Slow ResultBuffer</title>
      <link>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8401625#M24306</link>
      <description>&lt;P&gt;Nice solution.&lt;/P&gt;&lt;P&gt;Now I can save my large data using the ResultBuffer in 0.2 seconds instead of 12 seconds.&lt;/P&gt;&lt;P&gt;Thank you very much for the solution &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 15:28:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/slow-resultbuffer/m-p/8401625#M24306</guid>
      <dc:creator>office</dc:creator>
      <dc:date>2018-11-14T15:28:25Z</dc:date>
    </item>
  </channel>
</rss>

