Author Topic: ( Challenge ) How fast can you convert these?  (Read 27194 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ( Challenge ) How fast can you convert these?
« Reply #45 on: December 05, 2008, 07:37:21 PM »

Command: Test4
Ellapsed time: 0.0625 seconds

This had me a little excited till I realised that all the points were already converted to circles :D
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ( Challenge ) How fast can you convert these?
« Reply #46 on: December 05, 2008, 07:59:50 PM »
Stumbled across this:
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ( Challenge ) How fast can you convert these?
« Reply #47 on: December 05, 2008, 08:27:03 PM »

hey, Alan ... don't just sit there with an enigmatic smile on your face ; tell us the story :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ( Challenge ) How fast can you convert these?
« Reply #48 on: December 05, 2008, 08:38:54 PM »
I was trying to run down some info on reactors & stumbled into this in my library. 8-)

See pages 27+
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ( Challenge ) How fast can you convert these?
« Reply #49 on: December 05, 2008, 08:52:50 PM »

Thanks Alan.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8706
  • AKA Daniel
Re: ( Challenge ) How fast can you convert these?
« Reply #50 on: December 05, 2008, 09:16:31 PM »
Great stuff, Thanks Cab

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ( Challenge ) How fast can you convert these?
« Reply #51 on: December 05, 2008, 10:38:33 PM »
Right place at the right time. :-)
Still looking for more material on vLisp Object Reactors. All I've found are simple examples.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Spike Wilbury

  • Guest
Re: ( Challenge ) How fast can you convert these?
« Reply #52 on: December 05, 2008, 11:02:45 PM »
Here is my stab.-

Total run time: 0.078 seconds

(if I removed those lines marked with total run time)

Code: [Select]
static void PointsToCircles(void)
{
CLapseTime ltTotal;
AcDbBlockTableRecordPointer pBTR(acdbCurDwg()->currentSpaceId(), AcDb::kForWrite);
if (pBTR.openStatus() != Acad::eOk) return;
AcDbBlockTableRecordIterator* pIterator = NULL;
if (pBTR->newIterator(pIterator) != Acad::eOk) return;
for (pIterator->start(); !pIterator->done(); pIterator->step())
{
AcDbObjectId objId;
pIterator->getEntityId(objId);
//if (pIterator->getEntityId(objId) != Acad::eOk) return; // Total run time: 0.125 seconds
AcDbObjectPointer<AcDbPoint> pPoint(objId, AcDb::kForRead);
if(pPoint.openStatus() == Acad::eOk)
{
AcDbCircle *pCircle = new AcDbCircle(pPoint->position(), pPoint->normal(), 1.0);
if (pCircle && pBTR->appendAcDbEntity(pCircle) == Acad::eOk)
{
pCircle->close(); // Total run time: 0.078 seconds

pPoint->upgradeOpen(); // Total run time: 0.109 seconds
pPoint->erase();
}
else
delete pCircle; // Total run time: 0.094 seconds
}
}
delete pIterator;
acutPrintf(_T("\nTotal run time: %.3f seconds"), ltTotal.LapseTimeSeconds());
}

Spike Wilbury

  • Guest
Re: ( Challenge ) How fast can you convert these?
« Reply #53 on: December 06, 2008, 12:14:00 AM »
Here is my stab.-

Total run time: 0.078 seconds

(if I removed those lines marked with total run time)

Code: [Select]
static void PointsToCircles(void)
{
CLapseTime ltTotal;
AcDbBlockTableRecordPointer pBTR(acdbCurDwg()->currentSpaceId(), AcDb::kForWrite);
if (pBTR.openStatus() != Acad::eOk) return;
AcDbBlockTableRecordIterator* pIterator = NULL;
if (pBTR->newIterator(pIterator) != Acad::eOk) return;
for (pIterator->start(); !pIterator->done(); pIterator->step())
{
AcDbObjectId objId;
pIterator->getEntityId(objId);
//if (pIterator->getEntityId(objId) != Acad::eOk) return; // Total run time: 0.125 seconds
AcDbObjectPointer<AcDbPoint> pPoint(objId, AcDb::kForRead);
if(pPoint.openStatus() == Acad::eOk)
{
AcDbCircle *pCircle = new AcDbCircle(pPoint->position(), pPoint->normal(), 1.0);
if (pCircle && pBTR->appendAcDbEntity(pCircle) == Acad::eOk)
{
pCircle->close(); // Total run time: 0.078 seconds

pPoint->upgradeOpen(); // Total run time: 0.109 seconds
pPoint->erase();
}
else
delete pCircle; // Total run time: 0.094 seconds
}
}
delete pIterator;
acutPrintf(_T("\nTotal run time: %.3f seconds"), ltTotal.LapseTimeSeconds());
}

But, after running Paul code and including the erase of the point - his approach takes the same 0.094 seconds vs. 0.125 seconds ==== 0.031 dif.


Was a great excercise !

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: ( Challenge ) How fast can you convert these?
« Reply #54 on: December 06, 2008, 08:48:29 AM »
I make advantage of this challenge to keep on learning C# (even it's a LISP forum, sorry CAB).
I tried a way using a selection set rather than scanning all database, it seems to be a little faster (0.5 on my system).
Thanks to Kerry for all the nice little tricks he shows me.

Code: [Select]
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace Point2Circle
{
    public class MyClass
    {
        [CommandMethod("Test2")]
        public void Test2()
        {
            DateTime start = DateTime.Now;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            TypedValue[] tValues = { new TypedValue((int)DxfCode.Start, "POINT") };
            SelectionFilter selFilter = new SelectionFilter(tValues);
            PromptSelectionResult result = ed.SelectAll(selFilter);
            if (result.Status == PromptStatus.OK)
            {
                ObjectId[] selSet = result.Value.GetObjectIds();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    foreach (ObjectId objId in selSet)
                    {
                        DBPoint pt = tr.GetObject(objId, OpenMode.ForWrite) as DBPoint;
                        if (pt != null)
                        {
                            Circle c = new Circle(pt.Position, pt.Normal, 15.0);
                            pt.Erase();
                            btr.AppendEntity(c);
                            tr.AddNewlyCreatedDBObject(c, true);
                        }
                    }
                    tr.Commit();
                    TimeSpan time = (DateTime.Now - start);
                    ed.WriteMessage("Elapsed time: {0} seconds", (time.TotalSeconds));
                }
            }
        }
    }
}
« Last Edit: December 06, 2008, 10:00:26 AM by gile »
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ( Challenge ) How fast can you convert these?
« Reply #55 on: December 06, 2008, 09:09:24 AM »
I'm just jealous of your multilingual programmers. 8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: ( Challenge ) How fast can you convert these?
« Reply #56 on: December 06, 2008, 09:48:11 AM »
I'm very very much comfortable with LISP

Here's a little comparative between foreach, mapcar, repeat and while statements.

Quote
Commande: TEST_FOREACH
 < Elapsed time: 1.188000 seconds. >

Commande: TEST_MAPCAR
 < Elapsed time: 1.172000 seconds. >

Commande: TEST_REPEAT
 < Elapsed time: 1.140000 seconds. >

Commande: TEST_WHILE
 < Elapsed time: 1.125000 seconds. >

Code: [Select]
(defun c:test_foreach (/ time ss)
  (setq time (getvar 'millisecs))
  (and
    (setq ss (ssget "x" '((0 . "point"))))
    (foreach p (ssnamex ss)
      (entmakex
(list
  (cons 0 "CIRCLE")
  (assoc 10 (entget (cadr p)))
  (cons 40 15.0)
)
      )
      (entdel (cadr p))
    )
  )
  (princ
    (strcat "\n < Elapsed time: "
    (rtos (/ (- (getvar 'millisecs) time) 1000.0) 2 6)
    " seconds. > "
    )
  )
  (princ)
)

(defun c:test_mapcar (/ time ss)
  (setq time (getvar 'millisecs))
  (and (setq ss (ssget "_X" '((0 . "POINT"))))
       (mapcar '(lambda (x)
  (entmakex
    (list
      '(0 . "CIRCLE")
      (assoc 10 (entget (cadr x)))
      '(40 . 15.)
    )
  )
  (entdel (cadr x))
)
       (ssnamex ss)
       )
  )
  (princ
    (strcat "\n < Elapsed time: "
    (rtos (/ (- (getvar 'millisecs) time) 1000.0) 2 6)
    " seconds. > "
    )
  )
  (princ)
)

(defun c:test_repeat (/ time ss n pt)
  (setq time (getvar 'millisecs))
  (and
    (setq ss (ssget "x" '((0 . "POINT"))))
    (repeat (setq n (sslength ss))
      (setq pt (ssname ss (setq n (1- n))))
      (entmakex
(list
  (cons 0 "CIRCLE")
  (assoc 10 (entget pt))
  (cons 40 15.0)
)
      )
      (entdel pt)
    )
  )
  (princ
    (strcat "\n < Elapsed time: "
    (rtos (/ (- (getvar 'millisecs) time) 1000.0) 2 6)
    " seconds. > "
    )
  )
  (princ)
)

(defun c:test_while (/ time ss n pt)
  (setq time (getvar 'millisecs))
  (and (setq ss (ssget "_X" '((0 . "POINT"))))
       (setq n -1)
       (while (setq pt (ssname ss (setq n (1+ n))))
(entmakex
   (list
     '(0 . "CIRCLE")
     (assoc 10 (entget pt))
     '(40 . 15.)
   )
)
(entdel pt)
       )
  )
  (princ
    (strcat "\n < Elapsed time: "
    (rtos (/ (- (getvar 'millisecs) time) 1000.0) 2 6)
    " seconds. > "
    )
  )
  (princ)
)
Speaking English as a French Frog

ronjonp

  • Needs a day job
  • Posts: 7529
Re: ( Challenge ) How fast can you convert these?
« Reply #57 on: December 06, 2008, 04:00:37 PM »
Another variant for mapcar :)   < Elapsed time: 1.031000 seconds. >

Code: [Select]
(defun c:test_mapcar (/ time addcircle)

  (defun addcircle (e /)
    (entmakex (list
'(0 . "CIRCLE")
(assoc 10 (entget e))
'(40 . 15.)
      )
    )
    (entdel e)
  )

  (setq time (getvar 'millisecs))
  (mapcar 'addcircle
  (mapcar 'cadr (ssnamex (ssget "_X" '((0 . "POINT")))))
  )
  (princ
    (strcat "\n < Elapsed time: "
    (rtos (/ (- (getvar 'millisecs) time) 1000.0) 2 6)
    " seconds. > "
    )
  )
  (princ)
)
« Last Edit: December 06, 2008, 04:08:20 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8706
  • AKA Daniel
Re: ( Challenge ) How fast can you convert these?
« Reply #58 on: December 06, 2008, 09:22:59 PM »
Duration: 0.586035376004492 sec   :laugh:

Code: [Select]
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;

using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;

[assembly: CommandClass(typeof(ExecMethod.Commands))]
//++--
namespace ExecMethod
{
  public class Commands
  {
    [CommandMethod("doit")]
    public static void doit()
    {
      HiPerfTimer pt = new HiPerfTimer();
      pt.Start();
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      Database db = AcDb.HostApplicationServices.WorkingDatabase;
      AcDb.TransactionManager tm = db.TransactionManager;
      try
      {
        using (Transaction tr = tm.StartTransaction())
        {
          BlockTableRecord blockTableRecord =
            tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db),
                OpenMode.ForWrite) as BlockTableRecord;

          foreach (ObjectId entId in blockTableRecord)
          {
            DBPoint point = tr.GetObject
              (entId, OpenMode.ForWrite, false) as DBPoint;

            if (point != null)
            {
              Circle c = new Circle(point.Position, point.Normal, 15.0);
              blockTableRecord.AppendEntity(c);
              tr.AddNewlyCreatedDBObject(c, true);
              point.Erase();
            }
          }
          tr.Commit();
        }
        pt.Stop();
        ed.WriteMessage("Duration: {0} sec\n", pt.Duration);
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.Message);
        ed.WriteMessage(ex.StackTrace);
      }
    }
  }
}

ronjonp

  • Needs a day job
  • Posts: 7529
Re: ( Challenge ) How fast can you convert these?
« Reply #59 on: December 06, 2008, 10:47:12 PM »
Duration: 0.586035376004492 sec   :laugh:

Code: [Select]
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;

using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;

[assembly: CommandClass(typeof(ExecMethod.Commands))]
//++--
namespace ExecMethod
{
  public class Commands
  {
    [CommandMethod("doit")]
    public static void doit()
    {
      HiPerfTimer pt = new HiPerfTimer();
      pt.Start();
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      Database db = AcDb.HostApplicationServices.WorkingDatabase;
      AcDb.TransactionManager tm = db.TransactionManager;
      try
      {
        using (Transaction tr = tm.StartTransaction())
        {
          BlockTableRecord blockTableRecord =
            tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db),
                OpenMode.ForWrite) as BlockTableRecord;

          foreach (ObjectId entId in blockTableRecord)
          {
            DBPoint point = tr.GetObject
              (entId, OpenMode.ForWrite, false) as DBPoint;

            if (point != null)
            {
              Circle c = new Circle(point.Position, point.Normal, 15.0);
              blockTableRecord.AppendEntity(c);
              tr.AddNewlyCreatedDBObject(c, true);
              point.Erase();
            }
          }
          tr.Commit();
        }
        pt.Stop();
        ed.WriteMessage("Duration: {0} sec\n", pt.Duration);
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.Message);
        ed.WriteMessage(ex.StackTrace);
      }
    }
  }
}

Cheater...that's like bringing a gun to a knife fight  :-D

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC