Author Topic: ( Challenge ) Shortest & Longest Pline  (Read 13466 times)

0 Members and 1 Guest are viewing this topic.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: ( Challenge ) Shortest & Longest Pline
« Reply #15 on: February 06, 2013, 12:23:54 AM »
I think I know why this version is faster, is there anyone who knows?

This is the answer:
Empirically it doesn't appear to be the answer. Your previous code was faster:
Code: [Select]
(C:ALE_SHORTLONGESTPL) Program running time: 297 msecs.
(C:TESTFOO) Program running time: 218 msecs.
(C:SHORTLONGESTPLBYLYR) Program running time: 140 msecs.
(C:SHORTLONGESTPL) Program running time: 140 msecs.
(C:REMALLBUTLONGEST&SHORTESTBYLAYER) Program running time: 63 msecs.
(C:REMALLBUTLONGEST&SHORTEST1)
Longest :    Layer=RED;    Length =71652.0653
Shortest:    Layer=RED;    Length =243.2759
Program running time: 78 msecs.
(C:REMALLBUTLONGEST&SHORTEST)
Longest :    Layer=RED;    Length =71652.0653
Shortest:    Layer=RED;    Length =243.2759
Program running time: 1107 msecs.
I think mine using the vlax-curve functions should perform faster due to it never needing to entget or convert the ename to a vla-object. The entget route is obviously extremely slow since it gets all the data from each entity (including each vector). The vla method should be a lot quicker since it obtains the full length by only one method. But the vlax-curve method goes quicker since it's not converting the eename at all (and from previous tests it's faster working directly on enames than on vla-objects).

Another possibility might be that you're using the command function to erase and the remove the longest/shortest. This is recreating a selection set and then modifying it, not to mention passing stuff to the command-line. You might also want to turn off CmdEcho to get an extra miniscule bit faster so it doesn't display as much on the cmd-line. It seems (even from the lines thread) that using entdel's 2nd call to "unerase" the longest/shortest is actually a bit faster (also no need to fiddle with Undo control and sysvars).

But the reason Mac'Antonio's and mine is a bit faster than ronjonp's (methinks) is because his is doing 2 loops through the entire list of objects. Ours are only looping through the layer names and then only the objects on each of those layers individually - a lot less iterations.

Edit: BTW, anyone know if something similar to undelete can be done through vla? I'm thinking it might be possible to convert the selection set to a vla-selectionset, then iterate that so it need not convert enames to vla-objects. But then how to undelete the longest/shortest.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: ( Challenge ) Shortest & Longest Pline
« Reply #16 on: February 06, 2013, 01:00:44 AM »
Here's what I meant by stepping through a vla selection set:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:RemAllButLongest&ShortestByLayer1 (/ doc ss len long short)
  2.   (vlax-for lay (vla-get-Layers doc)
  3.     (if (and (setq ss (ssget "_X" (list '(0 . "LINE,POLYLINE,LWPOLYLINE") (cons 8 (vla-get-Name lay)))))
  4.              (> (sslength ss) 1))
  5.                    long '(nil . 0.0)
  6.                    short '(nil . 1.7976931348623158e308))
  7.         (vlax-for obj ss
  8.           (if (> (setq len (vla-get-Length obj)) (cdr long))
  9.             (setq long (cons obj len)))
  10.           (if (< len (cdr short))
  11.             (setq short (cons obj len))))
  12.         (vlax-invoke ss 'RemoveItems (mapcar 'car (list long short)))
  13.         (vla-Erase ss)
  14.         (vla-Delete ss))))
  15.   (princ))
Though it's still a touch slower than Marc'Antonio's due to it doing a  RevomeItems on the selection set through ActiveX:
Code: [Select]
(C:ALE_SHORTLONGESTPL) Program running time: 187 msecs.
(C:TESTFOO) Program running time: 203 msecs.
(C:SHORTLONGESTPLBYLYR) Program running time: 141 msecs.
(C:SHORTLONGESTPL) Program running time: 140 msecs.
(C:REMALLBUTLONGEST&SHORTESTBYLAYER) Program running time: 63 msecs.
(C:REMALLBUTLONGEST&SHORTESTBYLAYER1) Program running time: 156 msecs.
(C:REMALLBUTLONGEST&SHORTEST1)
Longest :    Layer=RED;    Length =71652.0653
Shortest:    Layer=RED;    Length =243.2759
Program running time: 78 msecs.
(C:REMALLBUTLONGEST&SHORTEST)
Longest :    Layer=RED;    Length =71652.0653
Shortest:    Layer=RED;    Length =243.2759
Program running time: 1139 msecs.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Patrick_35

  • Guest
Re: ( Challenge ) Shortest & Longest Pline
« Reply #17 on: February 06, 2013, 04:12:18 AM »
Hi

My version

Code: [Select]
(defun c:test(/ doc ent lst sel tab tmp)
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (vla-startundomark doc)
  (and (ssget "x" (list (cons 0 "*POLYLINE")))
    (progn
      (vlax-for ent (setq sel (vla-get-activeselectionset doc))
(setq lst (cons (list (vla-get-layer ent) (vla-get-length ent) ent) lst))
      )
      (vla-delete sel)
      (while lst
(setq tmp (vl-sort (vl-remove-if-not '(lambda(x) (eq (caar lst) (car x))) lst) '(lambda(a b)(< (cadr a) (cadr b))))
      tab (cons (append (car tmp) (last tmp)) tab)
      tmp (cdr (vl-remove (last tmp) tmp))
      lst (vl-remove-if '(lambda(x) (eq (caar lst) (car x))) lst)
)
(mapcar '(lambda(x)(vla-delete (caddr x))) tmp)
      )
    )
  )
  (princ tab)
  (vla-endundomark doc)
  (princ)
)

@+

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: ( Challenge ) Shortest & Longest Pline
« Reply #18 on: February 06, 2013, 06:05:39 AM »
Empirically it doesn't appear to be the answer. Your previous code was faster:
<clip>
inerb,

thanks for your long and interesting answer, IMHO i think there is also another
reason that justifies the difference later when I have time I will say my thoughts.
I am sorry to say that the tests do not correspond with mine, attached the command
line tests done with my AutoCAD 2010 Mech (Vanilla profile).
Each test was done by launching AutoCAD and opening the DWG then exiting from
AutoCAD completely, see txt attachment.

(time-it '(c:ALE_ShortLongestPL)); Program running time: 561 msecs.
(time-it '(c:RemAllButLongest&ShortestByLayer)) ; Program running time: 546 msecs.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: ( Challenge ) Shortest & Longest Pline
« Reply #19 on: February 06, 2013, 07:14:06 AM »
OK, don't know what's the issue on yours. I've done the test by closing ACad down fully, then opening that DWG again, loading & running one of the codes from VLIDE, then closing acad. Then re-open acad and the DWG and rerun the test on the next. These are the results I get:
Code: [Select]
_$ (time-it '(C:ALE_ShortLongestPL))
Program running time: 188 msecs.

_$ (time-it '(c:RemAllButLongest&ShortestByLayer))
Program running time: 94 msecs.
I'm running in ACA 2013 in vanilla mode.

Workstation:
i7-2600 @ 3.4GH
16GB Ram
64bit Win7 Pro
AMD Radeon HD 7800
Seagate Baracuda 500GB 7200RPM SATA3 (6GB/s)
Seagate FreeAgent GoFlex 1TB USB3

So I thought, perhaps due to me running it from VLIDE, but still similar scenario applies from command-line without opening VLIDE:
Code: [Select]
Command: (load "G:\\Documents\\AutoLisp Tests\\Longest Shortest Line\\LongShort.LSP")
C:REMALLBUTLONGEST&SHORTESTBYLAYER1
Command: (time-it '(C:ALE_ShortLongestPL))
...
Program running time: 140 msecs.

Code: [Select]
Command: (load "G:\\Documents\\AutoLisp Tests\\Longest Shortest Line\\LongShort.LSP")
C:REMALLBUTLONGEST&SHORTESTBYLAYER1
Command: (time-it '(c:RemAllButLongest&ShortestByLayer))
Program running time: 78 msecs.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: ( Challenge ) Shortest & Longest Pline
« Reply #20 on: February 06, 2013, 07:47:48 AM »
Could the following modifications perhaps shave off a few millisecs?

Code: [Select]
(defun c:RemAllButLongest&ShortestByLayerLeeMacMod ( / en in le ll ln se sl ss )
    (foreach la '((8 . "BLUE") (8 . "CYAN") (8 . "GREEN") (8 . "RED"))
        (setq ll -1.0  le nil
              sl 1e308 se nil
        )
        (if (setq ss (ssget "_X" (list '(0 . "LINE,POLYLINE,LWPOLYLINE") la)))
            (progn
                (repeat (setq in (sslength ss))
                    (setq en (ssname ss (setq in (1- in)))
                          ln (vlax-curve-getdistatparam en (vlax-curve-getendparam en))
                    )
                    (if (< ll ln)
                        (setq ll ln le en)
                    )
                    (if (< ln sl)
                        (setq sl ln se en)
                    )
                    (entdel en)
                )
                (entdel le)
                (entdel se)
            )
        )
    )
    (princ)
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: ( Challenge ) Shortest & Longest Pline
« Reply #21 on: February 06, 2013, 08:50:14 AM »
OK, don't know what's the issue on yours. I've done the test by closing ACad down fully, then opening that DWG again, loading & running one of the codes from VLIDE, then closing acad. Then re-open acad and the DWG and rerun the test on the next. These are the results I get:
Code: [Select]
_$ (time-it '(C:ALE_ShortLongestPL))
Program running time: 188 msecs.

_$ (time-it '(c:RemAllButLongest&ShortestByLayer))
Program running time: 94 msecs.
I'm running in ACA 2013 in vanilla mode.
Also test in Vlide, now I think test are very unreliable:

;start A2013 & open only RandomPlines.dwg
;
$ (LOAD "E:/__Temp/LongShort.lsp")
TIME-IT
_$ (vl-load-com)
_$ (time-it '(C:ALE_ShortLongestPL))

Program running time: 904 msecs.

(time-it '(C:ALE_ShortLongestPL))

;start A2013 & open only RandomPlines.dwg
_$ (LOAD "E:/__Temp/LongShort.lsp")
TIME-IT
_$
_$ (vl-load-com)
_$ (time-it '(c:RemAllButLongest&ShortestByLayer))

Program running time: 421 msecs.
_$

;-------------------------------------------------------------

;start A2010 & open only RandomPlines.dwg
$ (LOAD "E:/__Temp/LongShort.lsp")
TIME-IT
_$ (vl-load-com)
_$ (time-it '(C:ALE_ShortLongestPL))

Program running time: 421 msecs.

;start A2010 & open only RandomPlines.dwg
_$ (LOAD "E:/__Temp/LongShort.lsp")
TIME-IT
_$
_$ (vl-load-com)
_$
_$ (time-it '(c:RemAllButLongest&ShortestByLayer))

Program running time: 390 msecs.
_$


;start A2010 & open only RandomPlines.dwg > second Test
_$ (LOAD "E:/__Temp/LongShort.lsp")
TIME-IT
_$ (vl-load-com)
_$ (time-it '(c:RemAllButLongest&ShortestByLayer))

Program running time: 562 msecs.               <<<<<<<<<<<<<<
_$

ronjonp

  • Needs a day job
  • Posts: 7527
Re: ( Challenge ) Shortest & Longest Pline
« Reply #22 on: February 06, 2013, 09:25:29 AM »
Could the following modifications perhaps shave off a few millisecs?

Code: [Select]
(defun c:RemAllButLongest&ShortestByLayerLeeMacMod ( / en in le ll ln se sl ss )
    (foreach la '((8 . "BLUE") (8 . "CYAN") (8 . "GREEN") (8 . "RED"))
        (setq ll -1.0  le nil
              sl 1e308 se nil
        )
        (if (setq ss (ssget "_X" (list '(0 . "LINE,POLYLINE,LWPOLYLINE") la)))
            (progn
                (repeat (setq in (sslength ss))
                    (setq en (ssname ss (setq in (1- in)))
                          ln (vlax-curve-getdistatparam en (vlax-curve-getendparam en))
                    )
                    (if (< ll ln)
                        (setq ll ln le en)
                    )
                    (if (< ln sl)
                        (setq sl ln se en)
                    )
                    (entdel en)
                )
                (entdel le)
                (entdel se)
            )
        )
    )
    (princ)
)

(C:REMALLBUTLONGEST&SHORTESTBYLAYERLEEMACMOD) - 63 msecs.   :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: ( Challenge ) Shortest & Longest Pline
« Reply #23 on: February 06, 2013, 12:28:35 PM »
New version:
Code - Auto/Visual Lisp: [Select]
  1. (defun C:ALE_ShortLongestPLByLyr01 ( / VlaSst VlaObj LenVal LyrNam InfLtC
  2.                                             InfLtB InfLtG InfLtR InfTot EntLst)
  3.     (defun ALE_CompareLenght (ObjFor LenVal InfLst / TmpLst)
  4.       (setq TmpLst (eval InfLst))
  5.       (cond
  6.         ( (> LenVal (cadddr TmpLst))
  7.           (set InfLst (list (car TmpLst) (cadr TmpLst) ObjFor LenVal))
  8.         )
  9.         ( (< LenVal (cadr   TmpLst))
  10.           (set InfLst (list ObjFor LenVal (caddr TmpLst) (cadddr TmpLst)))
  11.         )
  12.       )
  13.     )
  14.     (if (setq VlaSst (ssget "_X" (list '(0 . "POLYLINE"))))
  15.       (progn
  16.         (setq
  17.           VlaObj (vlax-ename->vla-object (ssname VlaSst 0))
  18.           LenVal (vla-get-length VlaObj)
  19.           InfLtB (list VlaObj LenVal VlaObj LenVal)
  20.           InfLtC InfLtB InfLtG InfLtB InfLtR InfLtB
  21.         )
  22.         (vlax-for ObjFor VlaSst
  23.           (setq
  24.             LenVal (vla-get-length ObjFor)
  25.             LyrNam (vla-get-Layer ObjFor)
  26.           )
  27.           (cond
  28.             ( (eq LyrNam "BLUE")  (ALE_CompareLenght ObjFor LenVal 'InfLtB) )
  29.             ( (eq LyrNam "CYAN")  (ALE_CompareLenght ObjFor LenVal 'InfLtC) )
  30.             ( (eq LyrNam "GREEN") (ALE_CompareLenght ObjFor LenVal 'InfLtG) )
  31.             ( (eq LyrNam "RED")   (ALE_CompareLenght ObjFor LenVal 'InfLtR) )
  32.           )
  33.         )
  34.       )
  35.     )
  36.     (setq InfTot (append InfLtC InfLtB InfLtG InfLtR))
  37.     (while InfTot
  38.       (setq
  39.         EntLst (cons (vlax-vla-object->ename (car InfTot)) EntLst)
  40.         InfTot (cddr InfTot)
  41.       )
  42.     )
  43.     (command
  44.       "_.ERASE" "_ALL" "_R"
  45.       (ssadd (nth 7 EntLst) (ssadd (nth 6 EntLst) (ssadd (nth 5 EntLst)
  46.       (ssadd (nth 4 EntLst) (ssadd (cadddr EntLst) (ssadd (caddr EntLst)
  47.       (ssadd (cadr EntLst)  (ssadd (car EntLst))))))))) ""
  48.     )
  49. )

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: ( Challenge ) Shortest & Longest Pline
« Reply #24 on: February 06, 2013, 12:41:41 PM »
Could the following modifications perhaps shave off a few millisecs?

Code: [Select]
(defun c:RemAllButLongest&ShortestByLayerLeeMacMod < ... >
(C:REMALLBUTLONGEST&SHORTESTBYLAYERLEEMACMOD) - 63 msecs.   :-)

Your computer is way faster than mine  :-D

ronjonp

  • Needs a day job
  • Posts: 7527
Re: ( Challenge ) Shortest & Longest Pline
« Reply #25 on: February 06, 2013, 12:53:44 PM »
Could the following modifications perhaps shave off a few millisecs?

Code: [Select]
(defun c:RemAllButLongest&ShortestByLayerLeeMacMod < ... >
(C:REMALLBUTLONGEST&SHORTESTBYLAYERLEEMACMOD) - 63 msecs.   :-)

Your computer is way faster than mine  :-D

What was your time?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: ( Challenge ) Shortest & Longest Pline
« Reply #26 on: February 06, 2013, 12:59:09 PM »
Could the following modifications perhaps shave off a few millisecs?

Code: [Select]
(defun c:RemAllButLongest&ShortestByLayerLeeMacMod < ... >
(C:REMALLBUTLONGEST&SHORTESTBYLAYERLEEMACMOD) - 63 msecs.   :-)

Your computer is way faster than mine  :-D

What was your time?

About 110ms

LE3

  • Guest
Re: ( Challenge ) Shortest & Longest Pline
« Reply #27 on: February 06, 2013, 01:18:58 PM »
the best performance, i was able to get using a c# routine is 88msecs on a bylayer sorting:
Quote
Command: netload
Command: sp2
Add polylines to selection: Specify opposite corner: 1624 found
Add polylines to selection:
Elapsed=88msecs.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: ( Challenge ) Shortest & Longest Pline
« Reply #28 on: February 06, 2013, 01:21:07 PM »
Here are my results for all 'layer-by-layer' programs posted:

Code: [Select]
(C:REMALLBUTLONGEST&SHORTESTBYLAYER)          Program running time: 109 msecs.
(C:REMALLBUTLONGEST&SHORTESTBYLAYERLEEMACMOD) Program running time: 109 msecs.
(C:REMALLBUTLONGEST&SHORTESTBYLAYER1)         Program running time: 359 msecs.
(C:SHORTLONGESTPLBYLYR)                       Program running time: 405 msecs.
(C:TESTFOO)                                   Program running time: 499 msecs.
(C:PATRICK_35TEST)                            Program running time: 546 msecs.
(C:ALE_SHORTLONGESTPLBYLYR01)                 Program running time: 561 msecs.

I also added a (gc) after every function call for cleanup.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: ( Challenge ) Shortest & Longest Pline
« Reply #29 on: February 06, 2013, 01:22:05 PM »
the best performance, i was able to get using a c# routine is 88msecs on a bylayer sorting

But how fast do the AutoLISP solutions run on your system by comparison?