Author Topic: xref layer name conversion  (Read 10478 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: xref layer name conversion
« Reply #15 on: March 28, 2006, 03:26:13 PM »
For fun I benched all the variants --

Woooowhoooooo!!!

Mine was the slowest, but it was still something I just learned, so I thought it would be fun to post it.

Is this typical of recursive programs?  or was this a bad example?  Sorry for the thread hijacking.. :cry:
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #16 on: March 28, 2006, 06:29:07 PM »
Edit: Fixed typo in MP_StripPrefixRecEx.

As I noted in another thread, recursive functions typically represent convenience for the programmer, but more work for the computer.

Optimizing recursive functions is a big topic Tim, and not one I'm particularly qualified to author, but I would say that when writing recursive functions some of the things you have to do are eliminate redundant function calls and variable declarations.

For example -- pretending that vl-string-position doesn't have the optional "start from the end" argument let's write a recursive variant that employs a common technique: write core (recursive) functionality as a function that is initially invoked via a wrapper --

Code: [Select]
(defun MP_StripPrefixRecEx ( string code )

    ;;  Not to be called directly, only via wrapper
    ;;  function MP_StripPrefixRec.
    ;;
    ;;  Uses argument 'code' which is calculated
    ;;  using the ascii function only once, by the
    ;;  calling / parent code.
    ;;
    ;;  Uses global scope variable 'position' declared
    ;;  by the calling code (or fully global if the
    ;;  parent code does not declare said variable),
    ;;  so it doen't have to push more onto the stack
    ;;  with each successive iteration than is absolutely
    ;;  esssential.

    (if (setq position (vl-string-position code string))
        (MP_StripPrefixRecEx
            (substr string (+ 2 position))
            code
        )
        String
    )
)

Code: [Select]
(defun MP_StripPrefixRec ( string delim / position )

    ;;  I am the wrapper code, responsible for doing any
    ;;  preprocessing, e.g. the (ascii delim) call as well
    ;;  as declaring any variables that are used in the
    ;;  MP_StripPrefixRecEx function, like the 'position'
    ;;  variable.
    ;;
    ;;  Generally speaking this is bad, bad coding practice as
    ;;  it requires one function to have intimate knowledge
    ;;  of the inner workings of another function, breaking
    ;;  black box rules and making for all kinds of potential
    ;;  maintenance woes. In this case I'm saying "it's ok"
    ;;  because this function is written entirely in the
    ;;  context of the other -- the two work together.

    (MP_StripPrefixRecEx string (ascii delim))

)

Having spent the time to optimize a little bit, the actual benefits may not be realized until the recurse levels get a little deep, as the following illuminates --

Code: [Select]
Using testString = "123" ...

Elapsed milliseconds / relative speed for 32768 iteration(s):

    (TW_STRIPSTRING TESTSTRING "|")........1281 / 1.04 <fastest>
    (MP_STRIPPREFIXREC TESTSTRING "|").....1328 / 1.00 <slowest>

Using testString = "123|456" ...

Elapsed milliseconds / relative speed for 32768 iteration(s):

    (MP_STRIPPREFIXREC TESTSTRING "|").....1469 / 1.02 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........1500 / 1.00 <slowest>

Using testString = "123|456|789" ...

Elapsed milliseconds / relative speed for 32768 iteration(s):

    (MP_STRIPPREFIXREC TESTSTRING "|").....1562 / 1.02 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........1593 / 1.00 <slowest>

Using testString = "123|456|789|ABC" ...

Elapsed milliseconds / relative speed for 32768 iteration(s):

    (MP_STRIPPREFIXREC TESTSTRING "|").....1562 / 1.06 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........1656 / 1.00 <slowest>

Using testString = "123|456|789|ABC|DEF" ...

Elapsed milliseconds / relative speed for 32768 iteration(s):

    (MP_STRIPPREFIXREC TESTSTRING "|").....1593 / 1.12 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........1781 / 1.00 <slowest>

Using testString = "123|456|789|ABC|DEF|GHI|JKL|MNO|PQR" ...

Elapsed milliseconds / relative speed for 32768 iteration(s):

    (MP_STRIPPREFIXREC TESTSTRING "|").....1781 / 1.18 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........2093 / 1.00 <slowest>

Sorry, that's all I've got for now.
« Last Edit: March 28, 2006, 06:38:29 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: xref layer name conversion
« Reply #17 on: March 28, 2006, 06:43:01 PM »
Thanks Michael.  Something (more) to study.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Slim©

  • Needs a day job
  • Posts: 6566
  • The Dude Abides...
Re: xref layer name conversion
« Reply #18 on: March 28, 2006, 06:47:29 PM »
Sorry for the thread hijacking.. :cry:

Normal around here...

Ron, did you get the help you needed?  :-)
I drink beer and I know things....

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #19 on: March 28, 2006, 06:57:22 PM »
Oops, <big> sorry Ron.

Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #20 on: March 28, 2006, 07:38:41 PM »
Sorry for the thread hijacking.. :cry:

Normal around here...

Ron, did you get the help you needed?  :-)

I got all the help I needed and more....par usual. Thanks guys :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #21 on: May 08, 2006, 05:23:10 PM »
Show off  :-D just kidding....you make it look so easy.

Just trying to get you to look at the problem / solution another way .. and have some fun at the same time .. honest.

If you find the lambda stuff annoying here's a lambdaless equivalent --

Code: [Select]
(defun StripXrefPrefix ( string / position )
    (if (setq position (vl-string-position 124 string 0 t))
        (substr string (+ 2 position))
        string
    )
)

:)

MP,

How could this be modified to strip the string down to thesecond to last bar -->>

ABC|123|456|789 to 456|789

Thanks,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #22 on: May 08, 2006, 05:46:03 PM »
Not sure I understand. Could you please show original data and desired data, for example --

Original: "ABC|123|456|789 to 456|789"

Desired: "789 to 456|789"

Thanks.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #23 on: May 08, 2006, 06:46:55 PM »
MP,

Does this help?

Original: "ABC|123|456|789"

Desired: "456|789

Original: "098|ABC|123|456|789"

Desired: "456|789

Instead of stripping out all of the "|" it leaves the last one and the text up to the second to last.

Thanks,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #24 on: May 08, 2006, 07:48:49 PM »
Edit: Simplified / renamed functions, re-ordered post to make comparison easier to discern.

Apologies -- written very quick, very dirty.

I don't really know what to call these functions but figured the candidates are better than foo1 and foo2.

Nonetheless, observe --

Code: [Select]
(defun KeepNthPlus ( n string delim / i code indexes )
    ;;  n used in same context as nth function
    ;;  delim is a one character string
    (setq i 0 code (ascii delim))
    (while (setq i (vl-string-position code string i))
        (setq indexes (cons (setq i (1+ i)) indexes))
    )
    (cond
        (   (null indexes) nil)
        (   (< n (length indexes))
            (substr string (1+ (nth n (reverse indexes))))
        )
    )
)

Versus --   
           
Code: [Select]
(defun KeepReversedNthPlus ( n string delim / i code indexes )
    ;;  n used in same context as nth function
    ;;  but counting from the end forwards
    ;;  delim is a one character string
    (setq i 0 code (ascii delim))
    (while (setq i (vl-string-position code string i))
        (setq indexes (cons (setq i (1+ i)) indexes))
    )
    (cond
        (   (null indexes) nil)
        (   (< n (length indexes))
            (substr string (1+ (nth n indexes)))
        )
        (   string   )
    )
)

Set up some test data --

Code: [Select]
(setq testString "abc|def|ghi|jkl|mno")
Test KeepNthPlus --

Code: [Select]
(foreach i '(0 1 2 3 4)
    (princ
        (strcat
            "(KeepNthPlus "
            (itoa i)
            " \""
            testString
            "\" \"|\") => "
            (vl-prin1-to-string
                (KeepNthPlus i testString "|")
            )
            "\n"
        )
    )
    (princ)
)

Test KeepReversedNthPlus --

Code: [Select]
(foreach i '(0 1 2 3 4)
    (princ
        (strcat
            "(KeepReversedNthPlus "
            (itoa i)
            " \""
            testString
            "\" \"|\") => "
            (vl-prin1-to-string
                (KeepReversedNthPlus i testString "|")
            )
            "\n"
        )
    )
    (princ)
)

Test KeepNthPlus results --

Quote
(KeepNthPlus 0 "abc|def|ghi|jkl|mno" "|") => "def|ghi|jkl|mno"
(KeepNthPlus 1 "abc|def|ghi|jkl|mno" "|") => "ghi|jkl|mno"
(KeepNthPlus 2 "abc|def|ghi|jkl|mno" "|") => "jkl|mno"
(KeepNthPlus 3 "abc|def|ghi|jkl|mno" "|") => "mno"
(KeepNthPlus 4 "abc|def|ghi|jkl|mno" "|") => nil

Test KeepReversedNthPlus results --

Quote
(KeepReversedNthPlus 0 "abc|def|ghi|jkl|mno" "|") => "mno"
(KeepReversedNthPlus 1 "abc|def|ghi|jkl|mno" "|") => "jkl|mno"
(KeepReversedNthPlus 2 "abc|def|ghi|jkl|mno" "|") => "ghi|jkl|mno"
(KeepReversedNthPlus 3 "abc|def|ghi|jkl|mno" "|") => "def|ghi|jkl|mno"
(KeepReversedNthPlus 4 "abc|def|ghi|jkl|mno" "|") => "abc|def|ghi|jkl|mno"

Something to toy with, cheers.
« Last Edit: May 09, 2006, 08:06:15 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #25 on: May 09, 2006, 09:14:03 AM »
MP,

Your knowledge of writing code never ceases to amaze me. Whether it be simple or complicated you always find an elegant way to solve a problem :). This is exactly what I was looking for.

Thanks again,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #26 on: May 09, 2006, 12:27:14 PM »
Awefully nice of you to say Ron, that is the illusion I'm shooting for -- Thanks!

 :lol:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #27 on: May 09, 2006, 02:38:33 PM »
Awefully nice of you to say Ron, that is the illusion I'm shooting for -- Thanks!

 :lol:


 :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC