Author Topic: ( Challenge ) Shortest & Longest line  (Read 12094 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: ( Challenge ) Shortest & Longest line
« Reply #30 on: January 31, 2013, 09:15:04 AM »
With undo turned off, I get:
Code: [Select]
(PH:ALL1)        running time:  686 msecs.
(C:SHORTLONGEST) running time: 1311 msecs.

weird, I also tried the newly opened dwg...

Comando: _U
Comando A disattivato. Utilizzare il comando ANNULLA per attivarlo

Comando: (time-it '(ph:all1)) Program running time: 4181 msecs.

Comando: (time-it '(c:ShortLongest))Program running time: 2745 msecs.



Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: ( Challenge ) Shortest & Longest line
« Reply #31 on: January 31, 2013, 09:19:00 AM »
I found during my tests that if I did many tests in quick succession, they became increasingly slower, what once took a mere 1800ms was taking over 60000ms and it got progressively 30-50% slower with each run .. until I closed the drawing and reopened it.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: ( Challenge ) Shortest & Longest line
« Reply #32 on: January 31, 2013, 09:32:32 AM »
I found during my tests that if I did many tests in quick succession, they became increasingly slower, what once took a mere 1800ms was taking over 60000ms and it got progressively 30-50% slower with each run .. until I closed the drawing and reopened it.
Yes, is better to reopen DWG.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: ( Challenge ) Shortest & Longest line
« Reply #33 on: January 31, 2013, 09:35:51 AM »
Wow you'll have busy this morning.  :-)

...here we have a lot of work to do...  :cry:

;(time-it '(c:CAB:test))                  > Program running time: 5617 msecs.
;(time-it '(c:ShortLongestByLyr))  > Program running time: 5600 msecs.

Try better your version because I get only 4 lines...


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ( Challenge ) Shortest & Longest line
« Reply #34 on: January 31, 2013, 10:37:09 AM »
You're quite right, code updated.
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.

LE3

  • Guest
Re: ( Challenge ) Shortest & Longest line
« Reply #35 on: January 31, 2013, 11:30:36 AM »
was just able to get: Elapsed.TotalMilliseconds=1320.1172 - using c#, see my previous post earlier, including the source.

or by doing an small change: Elapsed.TotalMilliseconds=1265.0109

Code: [Select]
        [CommandMethod("SHOLON")]         
public void cmd_shortestLongestLines()
        {
            var e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            TypedValue[] tv = { new TypedValue((int)DxfCode.Start, "LINE") };
            var filter = new SelectionFilter(tv);
            var options = new PromptSelectionOptions
            {
                MessageForAdding = "\nAdd lines to selection",
                MessageForRemoval = "\nRemove lines from selection",
                AllowDuplicates = false,
                RejectObjectsFromNonCurrentSpace = true
            };
            var psr = e.GetSelection(options, filter);
            if (psr.Status != PromptStatus.OK) return;
            var sw = new Stopwatch();
            var pairs = new SortedList<double, Line>();
            using (var tr = e.Document.Database.TransactionManager.StartTransaction())
            {
                sw.Start();
                foreach (var id in psr.Value.GetObjectIds())
                {
                    var line = tr.GetObject(id, OpenMode.ForRead) as Line;
                    if (line != null)
                    {
                        pairs.Add(line.Length, line);
                    }
                }
                pairs.RemoveAt(0);
                pairs.RemoveAt(pairs.Count - 1);
                foreach (var pair in pairs)
                {
                    pair.Value.UpgradeOpen();
                    pair.Value.Erase();
                    //tr.GetObject(pair.Value, OpenMode.ForWrite).Erase();
                }
                sw.Stop();
                tr.Commit();
            }
            e.WriteMessage("\nElapsed.TotalMilliseconds={0} \n", sw.Elapsed.TotalMilliseconds);
        }

ok... playmode it is off. have fun!

Here it is a much better performance:

Quote
Command: sholon
Add lines to selection: Specify opposite corner: 38608 found
Add lines to selection:
Elapsed=364msecs short19.6269346852711long1528.88882854822

Code: [Select]
        [CommandMethod("SHOLON")]
        public void cmd_shortestLongestLines()
        {
            var e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            TypedValue[] tv = { new TypedValue((int)DxfCode.Start, "LINE") };
            var filter = new SelectionFilter(tv);
            var options = new PromptSelectionOptions
            {
                MessageForAdding = "\nAdd lines to selection",
                MessageForRemoval = "\nRemove lines from selection",
                AllowDuplicates = false,
                RejectObjectsFromNonCurrentSpace = true
            };
            var psr = e.GetSelection(options, filter);
            if (psr.Status != PromptStatus.OK) return;
            var sw = new Stopwatch();
            var pairs = new Dictionary<double, Line>();
            KeyValuePair<double, Line> longestLine;
            KeyValuePair<double, Line> shortestLine;
            using (var tr = e.Document.Database.TransactionManager.StartTransaction())
            {
                sw.Start();
                foreach (var id in psr.Value.GetObjectIds())
                {
                    var line = tr.GetObject(id, OpenMode.ForWrite) as Line;
                    pairs.Add(line.Length, line);
                    line.Erase();
                }
                longestLine = pairs.Aggregate((a, b) => b.Key > a.Key ? b : a);
                shortestLine = pairs.Aggregate((a, b) => b.Key < a.Key ? b : a);
                longestLine.Value.Erase(false);
                shortestLine.Value.Erase(false);
                sw.Stop();
                tr.Commit();
            }
            e.WriteMessage("\nElapsed={0}msecs short{1}long{2} \n", sw.ElapsedMilliseconds,
                shortestLine.Key.ToString(CultureInfo.InvariantCulture), longestLine.Key.ToString(CultureInfo.InvariantCulture));
        }
« Last Edit: February 06, 2013, 03:48:15 PM by LE »

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: ( Challenge ) Shortest & Longest line
« Reply #36 on: January 31, 2013, 12:04:35 PM »
Bravo Stefan!  :-)

Thank you Lee

weird, I also tried the newly opened dwg...

Comando: _U
Comando A disattivato. Utilizzare il comando ANNULLA per attivarlo

Comando: (time-it '(ph:all1)) Program running time: 4181 msecs.

Comando: (time-it '(c:ShortLongest))Program running time: 2745 msecs.

Weird for me too...
I've made all tests on this: Pentium Dual Core, 3.0 GHz, 2Gb Ram, Autocad 11, 32bit
Then I tried on this configuration: Intel i5 ( x64), 4Gb Ram, Autocad 2012, 64bit
Code: [Select]
(LM:SHORTLONG)    running time =      1232
(LM:SHORTLONG2)   running time =      1248
(THARWAT)         running time =     11981
(PH:ALL1)         running time =     20046
vlax-curve works awful slow on i5 or is something wrong with that computer. If someone had the same results, please let me know.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: ( Challenge ) Shortest & Longest line
« Reply #37 on: January 31, 2013, 12:08:38 PM »
vlax-curve works awful slow on i5 or is something wrong with that computer.

Maybe a 64-bit issue? ... I really hope not  :-(

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: ( Challenge ) Shortest & Longest line
« Reply #38 on: January 31, 2013, 12:20:17 PM »
Using Win7 64
Quote
(PH:ALL1)         running time =     390
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: ( Challenge ) Shortest & Longest line
« Reply #39 on: January 31, 2013, 12:37:19 PM »
Quote
Weird for me too...
I've made all tests on this: Pentium Dual Core, 3.0 GHz, 2Gb Ram, Autocad 11, 32bit
Then I tried on this configuration: Intel i5 ( x64), 4Gb Ram, Autocad 2012, 64bit
Code: [Select]
(LM:SHORTLONG)    running time =      1232
(LM:SHORTLONG2)   running time =      1248
(THARWAT)         running time =     11981
(PH:ALL1)         running time =     20046
vlax-curve works awful slow on i5 or is something wrong with that computer. If someone had the same results, please let me know.

I have AutoCAD 2013 - PC not new: Dell Studio XPS - Intel Core2Duo P7450 2.13GHz 4Gb Ram - Seven 64

Here, without reopen DWG (with oops):
Comando: (time-it '(c:ShortLongest))
Program running time: 3338 msecs.
Comando: OOPS
Comando: (gc)
nil
Comando: (gc)
nil
Comando: (time-it '(ph:all1))
Program running time: 4618 msecs.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ( Challenge ) Shortest & Longest line
« Reply #40 on: January 31, 2013, 01:03:56 PM »
Using Win7 64
Quote
(PH:ALL1)         running time =     390

See my sig for comp specs. Running vanilla ACAD2006
TIME-IT
(19.6269 1528.89)
Program running time: 234 msecs.
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.

pBe

  • Bull Frog
  • Posts: 402
Re: ( Challenge ) Shortest & Longest line
« Reply #41 on: January 31, 2013, 01:08:17 PM »

Guess we can throw in the 'Min / 'Max function in the mix


with (command "erase"...)

Code: [Select]
(defun c:Long&ShortOfIt ( / dx ls ss i e l_ s_)
(defun dx (d x)(cdr (assoc d (entget x))))
(defun Ls (m y)(list (assoc (apply m (mapcar 'car y)) y))) 
   (if (setq  ss (ssget "_x" '((0 . "LINE"))))
     (progn
         (repeat (setq i (sslength ss))
               (setq e (ssname ss (setq i (1- i)))
                              l_ (cons (list  (distance (dx 10 e )(dx 11 e)) e) l_)     
                              s_ (cons (car l_) s_))
         (setq l_ (ls 'max l_)
                 s_ (ls 'min s_))
           )
         (command "_erase" ss "")
           (entdel (cadr (car l_)))
         (entdel (cadr (car s_)))
       )
     )(princ)
  )

with entdel (entdel ....)

Code: [Select]
(defun c:Long&ShortOfItnc ( / dx ls ss i e l_ s_)
(defun dx (d x)(cdr (assoc d (entget x))))
(defun Ls (m y)(list (assoc (apply m (mapcar 'car y)) y))) 
   (if (setq  ss (ssget "_x" '((0 . "LINE"))))
     (progn
         (repeat (setq i (sslength ss))
               (setq e (ssname ss (setq i (1- i)))
                              l_ (cons (list  (distance (dx 10 e )(dx 11 e)) e) l_)     
                              s_ (cons (car l_) s_))
         (setq l_ (ls 'max l_)
               s_ (ls 'min s_))
           (entdel e)
           )
         
           (entdel (cadr (car l_)))
         (entdel (cadr (car s_)))
       )
     )(princ)
  )

« Last Edit: January 31, 2013, 10:12:35 PM by pBe »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: ( Challenge ) Shortest & Longest line
« Reply #42 on: January 31, 2013, 01:08:42 PM »
(PH:ALL1) Program running time: 656 msecs.

Windows 8 X64

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: ( Challenge ) Shortest & Longest line
« Reply #43 on: January 31, 2013, 01:17:47 PM »
...strange again, with Intel Core2Duo P7450 2.13GHz 4Gb Ram - Seven 64

AutoCAD 2013
;(time-it '(c:CAB:test))                  > Program running time: 5460 msecs.
;(time-it '(c:ShortLongestByLyr))  > Program running time: 5100 msecs.

AutoCAD 2010
;(time-it '(c:CAB:test))                  > Program running time: 2199 msecs.
;(time-it '(c:ShortLongestByLyr))  > Program running time: 1545 msecs.

pBe

  • Bull Frog
  • Posts: 402
Re: ( Challenge ) Shortest & Longest line
« Reply #44 on: January 31, 2013, 01:19:37 PM »
Try running that on this specs

Memory 1.00 GB
Intel Atom (TM) CPU N455 @1.66GHz

OS: Windows 7 Starter
CAD Version: Autocad 2009

I bet you wouldnt like what you see  ;D