Author Topic: -={ Challenge }=- Group List by Number  (Read 26857 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- Group List by Number
« Reply #15 on: March 11, 2010, 06:26:42 AM »
Thanks Alan  :-)  Nice code also  :-)

wizman

  • Bull Frog
  • Posts: 290
Re: -={ Challenge }=- Group List by Number
« Reply #16 on: March 12, 2010, 02:26:39 PM »
Here's another nth:


Code: [Select]
(defun wiz-grp (l n / a b c d)
    (while (Setq d l)
        (while (< (setq b (length a)) n)
            (setq a (cons (nth b d) a) l (cdr l)))
            (setq  c (cons (reverse a) c)  a nil))
            (reverse c))

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- Group List by Number
« Reply #17 on: March 12, 2010, 02:30:25 PM »
Nice one Wiz  ^-^

Loving the new avatar btw  :-)

wizman

  • Bull Frog
  • Posts: 290
Re: -={ Challenge }=- Group List by Number
« Reply #18 on: March 12, 2010, 02:48:52 PM »
Thanks Lee, Good codes from all too... :-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- Group List by Number
« Reply #19 on: March 13, 2010, 11:52:29 AM »
Hi,

I am late (I had not much time for lisping these days) but here're my 2 cents

Code: [Select]
(defun split (l n / sub)
  (defun sub (a n)
    (if (< 0 n)
      (cons (car a) (sub (setq l (cdr a)) (1- n)))
    )
  )
  (if l
    (cons (sub l n) (split l n))
  )
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- Group List by Number
« Reply #20 on: March 13, 2010, 11:54:14 AM »
Nice one Gile  :-)  You do love the recursive solutions  8-)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- Group List by Number
« Reply #21 on: March 13, 2010, 12:21:16 PM »
Here is the result I get:

Code: [Select]
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (GROUPBYNUM_MAC3 LST 2).....1357 / 1.40 <fastest>
    (WIZ-GRP LST 2).............1482 / 1.28
    (GBN_EV LST 2)..............1545 / 1.23
    (GROUPBYNUM_MAC2 LST 2).....1575 / 1.21
    (GROUPBYNUM_MAC1 LST 2).....1654 / 1.15
    (SPLIT_GILE LST 2)..........1700 / 1.12
    (GROUPBYNUM_VK LST 2).......1731 / 1.10
    (GBN_AJT LST 2).............1872 / 1.02
    (GBN_AJT2 LST 2)............1903 / 1.00 <slowest>

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Group List by Number
« Reply #22 on: March 13, 2010, 01:19:20 PM »
Judging the posted performances it's obvious to me you guys are testing using a list with pretty modest size, guessing < 100 items. Re-run your tests with a list of 10000 items and you will find a huge difference in performance between the algorithms. Also, you should vary the "group by" value. Subtitle: One test does paint an accurate picture of relative performances. Just sayin'. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- Group List by Number
« Reply #23 on: March 13, 2010, 01:50:54 PM »
You make a good point Michael, I shall try your recommendations  - thanks  :-)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- Group List by Number
« Reply #24 on: March 13, 2010, 02:03:32 PM »
Ok, retesting with Michael's suggestions:

List with 200 elements, Grouped by 2
Code: [Select]
  (setq a '(0 1 2 3 4 5 6 7 8 9))
  (repeat 20 (setq lst (append a lst)))

Code: [Select]
Benchmarking ...............Elapsed milliseconds / relative speed for 4096 iteration(s):

    (GROUPBYNUM_MAC3 LST 2).....1482 / 2.04 <fastest>
    (WIZ-GRP LST 2).............1872 / 1.62
    (GBN_EV LST 2)..............1950 / 1.55
    (GROUPBYNUM_MAC2 LST 2).....2028 / 1.49
    (SPLIT_GILE LST 2)..........2215 / 1.37
    (GROUPBYNUM_MAC1 LST 2).....2262 / 1.34
    (GROUPBYNUM_VK LST 2).......2496 / 1.21
    (GBN_AJT2 LST 2)............2902 / 1.04
    (GBN_AJT LST 2).............3027 / 1.00 <slowest>

List of 200 elements, Grouped by 5
Code: [Select]
Benchmarking ...............Elapsed milliseconds / relative speed for 4096 iteration(s):

    (GROUPBYNUM_MAC3 LST 5).....1201 / 2.09 <fastest>
    (GROUPBYNUM_MAC2 LST 5).....1388 / 1.81
    (GROUPBYNUM_MAC1 LST 5).....1482 / 1.70
    (GBN_EV LST 5)..............1498 / 1.68
    (WIZ-GRP LST 5).............1498 / 1.68
    (SPLIT_GILE LST 5)..........1591 / 1.58
    (GBN_AJT2 LST 5)............2106 / 1.19
    (GBN_AJT LST 5).............2122 / 1.18
    (GROUPBYNUM_VK LST 5).......2512 / 1.00 <slowest>

List of 200 elements, Grouped by 20
Code: [Select]
Benchmarking ...............Elapsed milliseconds / relative speed for 4096 iteration(s):

    (GROUPBYNUM_MAC3 LST 20).....1045 / 2.40 <fastest>
    (GROUPBYNUM_MAC2 LST 20).....1092 / 2.30
    (GROUPBYNUM_MAC1 LST 20).....1139 / 2.20
    (SPLIT_GILE LST 20)..........1263 / 1.99
    (GBN_EV LST 20)..............1295 / 1.94
    (WIZ-GRP LST 20).............1357 / 1.85
    (GBN_AJT LST 20).............1732 / 1.45
    (GBN_AJT2 LST 20)............1747 / 1.44
    (GROUPBYNUM_VK LST 20).......2511 / 1.00 <slowest>

Hopefully this is a more rounded test.

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: -={ Challenge }=- Group List by Number
« Reply #25 on: March 13, 2010, 02:18:22 PM »
if (length lst) is a multiple of n
Code: [Select]
(defun GroupByNum_Mac4 (l n / a b i)
  (setq l (reverse l))
  (while l
    (setq i n)
    (while (< 0 i)
      (setq a (cons (car l) a)
    l (cdr l)
    i (1- i)
      )
    )
    (setq b (cons a b)
  a nil
    )
  )
  b
)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Group List by Number
« Reply #26 on: March 13, 2010, 02:43:53 PM »
Try it with:

(setq lst '(0 1 2 3 4 5 6 7 8 9 0))
        
(repeat 10 (setq lst (append lst lst)))

(length lst)

11264

with a group length of say 16

the relative difference between the slowest and fastest will be more in the order to 40 to 50 times

let the weeping begin
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- Group List by Number
« Reply #27 on: March 13, 2010, 02:54:10 PM »
You need the fastest function for the huge list?
for this case, you need to forget about recursion, and use a specialized algorithm ...

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: -={ Challenge }=- Group List by Number
« Reply #28 on: March 13, 2010, 02:55:17 PM »
List Length = 210x10 = 10240,  Grouped by 16:

Code: [Select]
Benchmarking ..........Elapsed milliseconds / relative speed for 128 iteration(s):

    (GROUPBYNUM_MAC3 LST 16)......1607 / 22.80 <fastest>
    (GROUPBYNUM_MAC4 LST 16)......1623 / 22.58
    (GROUPBYNUM_MAC2 LST 16)......1700 / 21.56
    (GROUPBYNUM_MAC1 LST 16)......1841 / 19.90
    (SPLIT_GILE LST 16)...........1981 / 18.50
    (GBN_EV LST 16)...............2090 / 17.53
    (WIZ-GRP LST 16)..............2090 / 17.53
    (GROUPBYNUM_VK LST 16)........3978 / 9.21
    (GBN_AJT2 LST 16)............36598 / 1.00
    (GBN_AJT LST 16).............36645 / 1.00 <slowest>

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Group List by Number
« Reply #29 on: March 13, 2010, 03:14:03 PM »
Excellent. Now having done this one needs to consider context. What's the typical context this function would be used? Maybe you'll use the function to take the flat list of doubles returned by (vlax-get polyline 'coordinates) and convert instead to a list of points (because for whatever reason you were eschewing dxf data), grouping by 2 or 3 depending if it's a lightweight or heavyweight polyline. Is it reasonable to anticipate you could have a polyline with 3000 to 5000 vertices? Back to you Jim.
« Last Edit: March 13, 2010, 03:22:43 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst