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

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: ( Challenge ) How fast can you convert these?
« Reply #30 on: December 05, 2008, 03:16:21 PM »
Off topic, but where did "CLapseTime" come from?

From master Paul  .. attached to this thread
http://www.theswamp.org/index.php?topic=15784.msg305476#msg305476

Swift

  • Swamp Rat
  • Posts: 596
Re: ( Challenge ) How fast can you convert these?
« Reply #31 on: December 05, 2008, 03:20:29 PM »
Ah ok, Thanks Daniel

pkohut

  • Guest
Re: ( Challenge ) How fast can you convert these?
« Reply #32 on: December 05, 2008, 03:57:31 PM »
Daniel, for reference your latest runs 0.156 seconds on my system.
I hadn't noticed that it was just blasting throught the BTR and I
wrote one that does the same except without smart pointers, with
a run time of 0.094 seconds.

Code: [Select]
CLapseTime ltTotal;
AcDbDatabase * pDb = acdbHostApplicationServices()->workingDatabase();
AcDbBlockTableRecord * pRec;
if(acdbOpenAcDbObject((AcDbObject *&) pRec,pDb->currentSpaceId(), AcDb::kForWrite) == Acad::eOk)
{
  AcDbBlockTableRecordIterator * pIter;
  if(pRec->newIterator(pIter) == Acad::eOk) {
    for(pIter->start(); !pIter->done(); pIter->step()) {
      AcDbObjectId objId;
      AcDbEntity * pEnt;
      if(pIter->getEntity(pEnt, AcDb::kForRead) == Acad::eOk) {
        AcDbPoint * pPoint = AcDbPoint::cast(pEnt);
        if(pPoint) {
          AcDbCircle * pCircle = new AcDbCircle(pPoint->position(), pPoint->normal(), 15.0);
          if(pCircle) {
            if(pRec->appendAcDbEntity(pCircle) == Acad::eOk)
              pCircle->close();
            else
              delete pCircle;
          }
        }
        pEnt->close();
      }
    }
    delete pIter;
  }
  pRec->close();
}
   acutPrintf(_T("\nTotal run time: %.3f seconds"), ltTotal.LapseTimeSeconds());


It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: ( Challenge ) How fast can you convert these?
« Reply #33 on: December 05, 2008, 05:12:26 PM »
Dang! Thatís fast. I was considering using pointers, but I didnít want to rub-it-in  :evil:

gile

  • Water Moccasin
  • Posts: 2231
  • Marseille, France
Re: ( Challenge ) How fast can you convert these?
« Reply #34 on: December 05, 2008, 05:19:37 PM »
Now wait to see the C# (.NET) version..... (hope someone post a sample too).... :)

As there's no more place lisping, I try something, but I'm newbie with C#...

EDIT : follow Kerry's suggestion and add a chrono: 0.563 second.

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
    {
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        Database db = HostApplicationServices.WorkingDatabase;
        [CommandMethod("Test")]
        public void Test()
        {
            DateTime start = DateTime.Now;
            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 btr)
                {
                    Entity ent = (Entity)tr.GetObject(objId, OpenMode.ForWrite);
                    DBPoint pt = ent as DBPoint;
                    if (pt != null)
                    {
                        Circle c = new Circle(pt.Position, pt.Normal, 15.0);
                        ent.Erase();
                        btr.AppendEntity(c);
                        tr.AddNewlyCreatedDBObject(c, true);
                    }
                }
                tr.Commit();
                DateTime stop = DateTime.Now;
                double time = (stop.Second + stop.Millisecond / 1000.0) - (start.Second + start.Millisecond / 1000.0);
                ed.WriteMessage("Ellapsed time: {0} seconds", time);
            }
        }
    }
}
« Last Edit: December 05, 2008, 06:45:11 PM by gile »
Speaking English as a French Frog

Kerry

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

Nice start gile :)

Perhaps the routine could be more efficient if the 'is' methodology was replaced with 'as' methodology.

I have some work to finish, but I'd like to play with this later as well.

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

--> Donate to theSwamp<--

gile

  • Water Moccasin
  • Posts: 2231
  • Marseille, France
Re: ( Challenge ) How fast can you convert these?
« Reply #36 on: December 05, 2008, 06:23:28 PM »
Thanks Kerry,

I edited the code according to what you suggested (if I'd understood ?) and add a chrono.
It seems to be far away behind the C++ codes, but I'm quite proud: it works :kewl:
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ( Challenge ) How fast can you convert these?
« Reply #37 on: December 05, 2008, 06:25:34 PM »
That looks better gile .. can you repost the previous code, just for a comparison ( please :) )
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

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


:)  to be fair though, the
DateTime start = DateTime.Now;
probably could be higher up the process chain :)
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

gile

  • Water Moccasin
  • Posts: 2231
  • Marseille, France
Re: ( Challenge ) How fast can you convert these?
« Reply #39 on: December 05, 2008, 06:43:11 PM »
Quote
That looks better gile .. can you repost the previous code, just for a comparison  :-)

I just replaced:

if (ent is DBPoint) {DBPoint pt = ent as DBPoint; ...}

by

DBPoint pt = ent as DBPoint;
if (pt != null) {...}

PS: i put the start chrono higher, it doesn't change, the times are still between 0.53 and 0.58
Speaking English as a French Frog

SomeCallMeDave

  • Guest
Re: ( Challenge ) How fast can you convert these?
« Reply #40 on: December 05, 2008, 06:55:11 PM »
</start Rant of drunk guy that writes slow code>

OK,  just to open up another topic of discussion: 
                 Why do all the "How fast...." threads always use elapsed run time as the basis of comparison?

For my money (and my clients' money),  a snippet of code that takes 3 minutes to write and 1.5 seconds to execute is worth a whole lot more than a snippet of code that takes 29.37 minutes to write, compile, and load  and 0.047 seconds to execute.

I think that the time measurement should be started when the problem explanation is completed and stopped when the required result is delivered to the client.

And I understand that if a program is going to be run thousands of times, execution time will matter.  But in the CAD world,  how many times does that happen.

But even in the 'made-up' example above,  the faster program would have to execute about 34,000 times just to 'break even'.  Not very many of my routines run that many times.

</end Rant of drunk guy that writes slow code>


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ( Challenge ) How fast can you convert these?
« Reply #41 on: December 05, 2008, 07:06:44 PM »
Command: netload
Command: test1
Ellapsed time: 0.875 seconds
Command: u

Command: test1
Ellapsed time: 0.78125 seconds
Command: u

Command: test2
Ellapsed time: 0.78125 seconds

ps: I added a line to the drawing to make sure it was selective ok :)

Code: [Select]
[assembly: AcRx.CommandClass(typeof(kdub.Testing.Commands.ConvertPoints))]

namespace kdub.Testing.Commands
{
    /// <summary>
    /// Summary description for Routine.
    /// </summary>
    public class ConvertPoints
    {
        [AcRx.CommandMethod("Test1")]
        static public void Test1()
        {
            DateTime timeStart = DateTime.Now;
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;

            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 btr)
                {
                    Entity ent = (Entity)tr.GetObject(objId, OpenMode.ForWrite);
                    DBPoint pt = ent as DBPoint;
                    if (pt != null)
                    {
                        Circle c = new Circle(pt.Position, pt.Normal, 15.0);
                        ent.Erase();
                        btr.AppendEntity(c);
                        tr.AddNewlyCreatedDBObject(c, true);
                    }
                }
                tr.Commit();

            }
            TimeSpan TimeDuration = (DateTime.Now - timeStart);
            ed.WriteMessage("Ellapsed time: {0} seconds", (TimeDuration.TotalSeconds));
        }

        [AcRx.CommandMethod("Test2")]
        static public void Test2()
        {
            DateTime timeStart = DateTime.Now;
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
                                        as BlockTableRecord;
                foreach (ObjectId objId in btr)
                {                   
                    // make the assignment more direct
                    //
                    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 TimeDuration = (DateTime.Now - timeStart);
            ed.WriteMessage("Ellapsed time: {0} seconds", (TimeDuration.TotalSeconds ));
        }
    }
}

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

--> Donate to theSwamp<--

pkohut

  • Guest
Re: ( Challenge ) How fast can you convert these?
« Reply #42 on: December 05, 2008, 07:18:41 PM »
Ah, cause it is called a challenge and it's an opportunity for people to explore
and learn from others using small well defined problem space.

Cheers!!
Paul

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: ( Challenge ) How fast can you convert these?
« Reply #43 on: December 05, 2008, 07:21:05 PM »
Cheers! again  :lol:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ( Challenge ) How fast can you convert these?
« Reply #44 on: December 05, 2008, 07:26:34 PM »
David,
I usually don't spend a lot of time looking for the extra 0.01 of a second ... I understand all about a return on investment when writing code.

but, as Paul indicated so succintly , these are ' an opportunity for people to explore and learn from others '
not an appendage measuring competition :)
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--