(setq num_pl 0)
(setq num 100)
(while (< num_pl num)
(if (< num_pl num)
(progn
(...pt01)
)
)
(command "_.change" el "" "_p" "_c" col_pl "")
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt02)
)
)
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt03)
)
)
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt04)
)
)
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt05)
)
)
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt06)
)
)
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt07)
)
)
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt08)
)
)
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt09)
)
)
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt10)
)
)
(setq num_pl (1+ num_pl))
(if (< num_pl num)
(progn
(...pt11)
)
)
)
(...pt01)
(...pt02)
...
(setq n 0)
(foreach n lst1
(... (nth (rem n 11) lst2)) ; instead of pt0n
(setq n (1+ n))
)
(defun c:test(/ idx)
(defun do_something (x y)
(print (+ x y))
)
(setq listA '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)
listB '(1 2 3 4 5 6 7 8 9 10 11)
)
(setq MaxListA 10) ; max number to process, why I don't know
(setq idx 0) ; index pointer to listA
(setq MaxListA (1- MaxListA)) ; make zero based
(while
(and
(< idx MaxListA) ; did not exceed the max allowed
(setq itm (nth idx ListA)) ; got a valid item
)
;; OK to process the item - 11 times from ListB
(foreach B_item listB
(do_something itm B_item)
)
(setq idx (1+ idx))
) ; endwhile
(princ)
)
(1 1) (2 2) (3 3) (4 4) (5 1) (6 2) (7 3) (8 4) (9 1) (10 2) (11 3) (1 4) (2 1) (3 2) ...
(defun c:test(/ idxA idxB)
(defun do_something (x y)
(print (list x y))
)
(setq lista '("a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l")
listB '(1 2 3 4 5 6 7 8 9 10 11)
)
(setq MaxPairs 20) ; max number to process, why I don't know
(setq cnt 0 ; counter
idxA 0 ; index pointer
idxB 0 ; index pointer
lenA (length ListA) ; length of list
lenB (length ListB)
)
(while (< (setq cnt (1+ cnt)) MaxPairs)
(do_something (nth idxA ListA) (nth idxB ListB))
(setq idxB (1+ idxB))
(if (= idxB lenB)
(progn
(setq idxA (1+ idxA)
idxB 0)
(if (= idxA lenA)
(setq inxA 0)
)
)
)
) ; endwhile
(princ)
)
_$ (pairs '(1 2 3 4 5 6 7 8 9 10 11) '(1 2 3 4) 15)
((1 1) (2 2) (3 3) (4 4) (5 1) (6 2) (7 3) (8 4) (9 1) (10 2) (11 3) (1 4) (2 1) (3 2) (4 3))
(defun paircab (lista listb maxpairs / idxa idxb lena lenb cnt)
(setq cnt 0 ; counter
idxa 0 ; index pointer
idxb 0 ; index pointer
lena (length lista) ; length of list
lenb (length listb)
)
(while (< (setq cnt (1+ cnt)) maxpairs)
(setq lst (cons (list (nth idxa lista) (nth idxb listb)) lst))
(setq idxb (1+ idxb))
(if (= idxb lenb)
(progn
(setq idxa (1+ idxa) idxb 0)
(if (= idxa lena)
(setq inxa 0)
)
)
)
) ; endwhile
(reverse lst)
)
If you are sill awake over there how does my clunker hold up?
_$ (paircab '(1 2 3) '("a" "b") 8)
((1 "a") (1 "b") (2 "a") (2 "b") (3 "a") (3 "b") (nil "a"))
_$ (pairup7 '(1 2 3) '("a" "b") 8)
((1 "a") (2 "b") (3 "a") (1 "b") (2 "a") (3 "b") (1 "a") (2 "b"))
_$ (setq l1 '(1 2 3 4 5 6 7) l2 '("a" "b" "c" "d" "e") n 200)
200
_$ (QuickBench (mapcar '(lambda (f) (list f 'l1 'l2 'n)) '(PAIRUP5 PAIRUP6 PAIRUP7 PAIRUP8 PAIRLISTS paircab)))
Benchmarking ...... done for 8192 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRLISTS L1 L2 N) 8192 1701 1701 27.30
(PAIRUP7 L1 L2 N) 4096 1387 2774 16.74
(PAIRUP8 L1 L2 N) 4096 1388 2776 16.73
(PAIRUP6 L1 L2 N) 4096 1763 3526 13.17
(PAIRUP5 L1 L2 N) 4096 1980 3960 11.73
(PAIRCAB L1 L2 N) 256 1451 46432 1.00
--------------------------------------------------------------------------------
And it gives the same result:_$ (equal (PairLists l1 l2 n) (pairup5 l1 l2 n))
T
(defun paircab (lista listb maxpairs / idxa idxb lena lenb cnt lst)
(setq cnt 0
idxa 0
idxb 0
lena (length lista)
lenb (length listb)
)
(while (<= (setq cnt (1+ cnt)) maxpairs)
(setq lst (cons (list (nth idxa lista) (nth idxb listb)) lst))
(if (= (setq idxb (1+ idxb)) lenb)
(progn
(setq idxa (1+ idxa) idxb 0)
(if (= idxa lena) (setq idxa 0))))
) ; endwhile
(reverse lst)
)
Benchmarking [M.P. 2005] ...............Elapsed milliseconds for 4096 iteration(s)/ relative Timing :
(PAIRUP1 L1 L2 N).....3401 / 4.0344 <slowest>
(PAIRUP3 L1 L2 N).....2886 / 3.4235
(PAIRS L1 L2 N).......1544 / 1.8316
(PAIRCAB L1 L2 N).....1497 / 1.7758
(PAIRUP4 L1 L2 N).....1357 / 1.6097
(PAIRUP2 L1 L2 N).....1248 / 1.4804
(PAIRUP5 L1 L2 N).....1186 / 1.4069
(PAIRUP6 L1 L2 N).....1014 / 1.2028
(PAIRUP7 L1 L2 N)......858 / 1.0178
(PAIRUP8 L1 L2 N)......843 / 1.0000 <fastest>
_$ (_iterEnd pairup6 l1 l2 8)
((1 "a"))
((1 "a") (2 "b"))
((1 "a") (2 "b") (3 "c"))
((1 "a") (2 "b") (3 "c") (4 "d"))
((1 "a") (2 "b") (3 "c") (4 "d") (5 "e"))
((1 "a") (2 "b") (3 "c") (4 "d") (5 "e") (6 "a"))
((1 "a") (2 "b") (3 "c") (4 "d") (5 "e") (6 "a") (7 "b"))
((1 "a") (2 "b") (3 "c") (4 "d") (5 "e") (6 "a") (7 "b") (1 "c"))
End at 8
_$ (_iterEnd paircab l1 l2 8)
((1 "a"))
((1 "a") (1 "b"))
((1 "a") (1 "b") (1 "c"))
((1 "a") (1 "b") (1 "c") (1 "d"))
((1 "a") (1 "b") (1 "c") (1 "d") (1 "e"))
((1 "a") (1 "b") (1 "c") (1 "d") (1 "e") (2 "a"))
((1 "a") (1 "b") (1 "c") (1 "d") (1 "e") (2 "a") (2 "b"))
((1 "a") (1 "b") (1 "c") (1 "d") (1 "e") (2 "a") (2 "b") (2 "c"))
End at 8
_$ (_iterEnd PairLists l1 l2 8)
((1 "a"))
((1 "a") (2 "b"))
((1 "a") (2 "b") (3 "c"))
((1 "a") (2 "b") (3 "c") (4 "d"))
End at 4
_$
Benchmarking ............ done for 4096 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRLISTS2 L1 L2 N) 4096 1030 1030 4.97
(PAIRLISTS L1 L2 N) 4096 1060 1060 4.83
(PAIRUP8 L1 L2 N) 4096 1485 1485 3.45
(PAIRUP7 L1 L2 N) 4096 1575 1575 3.25
(PAIRUP6 L1 L2 N) 4096 1842 1842 2.78
(PAIRUP5 L1 L2 N) 2048 1030 2060 2.49
(PAIRUP2 L1 L2 N) 2048 1139 2278 2.25
(PAIRUP4 L1 L2 N) 2048 1184 2368 2.16
(PAIRS L1 L2 N) 2048 1325 2650 1.93
(PAIRCAB L1 L2 N) 2048 1325 2650 1.93
(PAIRUP3 L1 L2 N) 1024 1200 4800 1.07
(PAIRUP1 L1 L2 N) 1024 1280 5120 1.00
--------------------------------------------------------------------------------
Benchmarking ............ done for 8192 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRLISTS L1 L2 N) 8192 1872 1872 4.37
(PAIRLISTS2 L1 L2 N) 8192 1888 1888 4.33
(PAIRUP8 L1 L2 N) 4096 1389 2778 2.94
(PAIRUP7 L1 L2 N) 4096 1405 2810 2.91
(PAIRUP4 L1 L2 N) 4096 1420 2840 2.88
(PAIRUP6 L1 L2 N) 4096 1435 2870 2.85
(PAIRS L1 L2 N) 4096 1622 3244 2.52
(PAIRCAB L1 L2 N) 4096 1809 3618 2.26
(PAIRUP2 L1 L2 N) 2048 1046 4184 1.95
(PAIRUP5 L1 L2 N) 2048 1062 4248 1.92
(PAIRUP3 L1 L2 N) 2048 1997 7988 1.02
(PAIRUP1 L1 L2 N) 2048 2043 8172 1.00
--------------------------------------------------------------------------------
Here's the fix:Code - Auto/Visual Lisp: [Select]
Nice recover :)A situation of trying to make it too elegant and forgetting borderline situations like binary-exponent factor lengths :pissed: - making for repeat 0 times --> nil.
Benchmarking .. done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRUP5 L1 L2 N) 2048 1108 1108 1.10
(PAIRUP5A L1 L2 N) 2048 1216 1216 1.00
--------------------------------------------------------------------------------
Actually looks like it less efficient doesn't it ... but wait:Benchmarking .. done for 4096 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRUP5A L1 L2 N) 4096 1981 1981 1.17
(PAIRUP5 L1 L2 N) 2048 1156 2312 1.00
--------------------------------------------------------------------------------
Although it still doesn't gain enough to move it up in the list.
_$ (setq l1 '(1 2 3 4 5 6 7) l2 '("a" "b" "c" "d" "e") n 200)
200
_$
; 3 forms loaded from #<editor "J:/Documents/Benchmark.lsp">
_$ (QuickBench (mapcar '(lambda (f) (list f 'l1 'l2 'n)) '(PAIRUP7 PairLists2 PairLists3)))
Benchmarking ... done for 4096 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRLISTS3 L1 L2 N) 4096 1560 1560 1.30
(PAIRLISTS2 L1 L2 N) 4096 1607 1607 1.26
(PAIRUP7 L1 L2 N) 4096 2027 2027 1.00
--------------------------------------------------------------------------------
_$ (setq l1a '(1 2) l2a '("a" "b") n 257)
257
_$ (QuickBench (mapcar '(lambda (f) (list f 'l1a 'l2a 'n)) '(PAIRUP7 PairLists2 PairLists3)))
Benchmarking ... done for 4096 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRLISTS3 L1A L2A N) 4096 1701 1701 2.18
(PAIRUP7 L1A L2A N) 2048 1373 2746 1.35
(PAIRLISTS2 L1A L2A N) 2048 1855 3710 1.00
--------------------------------------------------------------------------------
Probably since there's no need for the final revers + repeat cdr anymore - that is handled in the make-list-from on much smaller lists.
_$ (setq l1 '(1 2 3 4 5 6 7) l2 '("a" "b" "c" "d" "e") n 200)
200
_$ (QuickBench (mapcar '(lambda (f) (list f 'l1 'l2 'n)) '(PAIRUP7 PairUp9 PairLists2 PairLists3 eea-pairup paircab pairs)))
Benchmarking ....... done for 4096 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRUP9 L1 L2 N) 4096 1108 1108 2.68
(PAIRLISTS3 L1 L2 N) 4096 1278 1278 2.32
(PAIRLISTS2 L1 L2 N) 4096 1326 1326 2.24
(PAIRUP7 L1 L2 N) 4096 1793 1793 1.65
(PAIRS L1 L2 N) 2048 1436 2872 1.03
(PAIRCAB L1 L2 N) 2048 1467 2934 1.01
(EEA-PAIRUP L1 L2 N) 2048 1483 2966 1.00
--------------------------------------------------------------------------------
_$ (setq l1 '(1 2) l2 '("a" "b") n 257)
257
_$ (QuickBench (mapcar '(lambda (f) (list f 'l1 'l2 'n)) '(PAIRUP7 PairUp9 PairLists2 PairLists3 eea-pairup paircab pairs)))
Benchmarking ....... done for 4096 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRLISTS3 L1 L2 N) 4096 1450 1450 3.10
(PAIRUP9 L1 L2 N) 4096 1498 1498 3.00
(PAIRUP7 L1 L2 N) 2048 1123 2246 2.00
(PAIRLISTS2 L1 L2 N) 2048 1560 3120 1.44
(PAIRS L1 L2 N) 2048 1810 3620 1.24
(PAIRCAB L1 L2 N) 2048 1997 3994 1.12
(EEA-PAIRUP L1 L2 N) 1024 1123 4492 1.00
--------------------------------------------------------------------------------
I think a slight mod on PairUp9 might make it the quickest overall. Something like checking which of l1 & l2 would give the closest match to the specified length by doubling it each time - would have to look at the math on this to not detract on the performance too much. Yet perhaps it might still require an option where both lists are run through the make-list-from function if both produce a list much longer than required.
That's a very novel one EE!
Lee. I hope you don't mind. I had to swap the arguments around on that make-list-from defun ... I prefer sticking with the CL's argument order and not mix it up.
I think a slight mod on PairUp9 might make it the quickest overall. Something like checking which of l1 & l2 would give the closest match to the specified length by doubling it each time - would have to look at the math on this to not detract on the performance too much.
(defun pow2 (x n)
(rem (/ (log (/ n x 1.0)) (log 2)) 1)
)
(defun make-list-from ( n l / x )
(cond
( (<= n 0)
nil
)
( (>= n (length l))
(setq x l)
(while (< (* (length x) 2) n) (setq x (append x x)))
(append x (make-list-from (- n (length x)) l))
)
( (setq l (reverse l))
(reverse (repeat (- (length l) n) (setq l (cdr l))))
)
)
)
Note that the performance difference between the functions is highly dependent upon the combination of list length and number of pairs; for example, in the test case for all benchmarking performed in this thread we have used:Code - Auto/Visual Lisp: [Select]
(setq l1 '(1 2 3 4 5 6 7) l2 '("a" "b" "c" "d" "e") n 200 )
In Irneb's function, following the exponential doubling operation, the initial lists have lengths 224 and 320 respectively (7x25 | 5x26), meaning that only 24 items need be removed after the mapcar pairing.
Conversely, in my pairup7 code, after the compounding of the restricting list 'l3', 72 items need to be added to obtain the correct length.
If we now skew these parameters, so that the lists both have length 2, and the number of pairs is 257, Irneb's function needs to remove 255 items from the resulting paired list (29 - 257); whereas, my pairup7 function only need add 1 item following the successive doubling.
Hello, Thank you for your discussion, I have followed with interest, for functions pow2 and make-list-from, if you want, I offer both small simplifications...For the make-list-from, your optimization seems to give a 2% increase in speed generally, but on the conditions where the length of the sample list has a binary factor congruent to the final size it's actually 6% slower (and the larger the size the smaller the efficiency difference):
As far as the pairing is concerned, the first list is a set of integers which could vary in length from 1 to 100. The second list is a point list with exactly 11 lists of 3 reals. This length will never change. The counter will realistically be no less than 200, but more in the range of 750 to 1000. Perhaps these realistic figures will help to give a more accurate benchmark of the functions.Well to see the benchmarking on all the functions to those values, use the attached's last function (PairupTest):
It is a slightly smaller defun though, and the efficiency is so similar as to be preferable. So perhaps I'll adopt your take on it.
(defun make-list-from (n l / x)
(cond
((>= n (length l))
(setq x l)
(while (< (* (length x) 2) n) (setq x (append x x)))
(append x (make-list-from (- n (length x)) l))
)
((> n 0)
(setq l (reverse l))
(reverse (repeat (- (length l) n) (setq l (cdr l))))
)
)
)
(setq l (reverse l))
(reverse (repeat (- (length l) n) (setq l (cdr l))))
_$ (PairupTest)
Testing with int list of 95 length, and counter of 991
Benchmarking .................. done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(PAIRS1 L1 L2 N) 2048 1500 1500 26.33
(PAIRS2 L1 L2 N) 2048 1608 1608 24.56
(PAIRUP10 L1 L2 N) 2048 1937 1937 20.39
(PAIRLISTS3 L1 L2 N) 1024 1048 2096 18.84
(PAIRUP9 L1 L2 N) 1024 1062 2124 18.59
(PAIRLISTS L1 L2 N) 1024 1686 3372 11.71
(PAIRLISTS2 L1 L2 N) 1024 1702 3404 11.60
(PAIRUP7 L1 L2 N) 512 1093 4372 9.03
(PAIRUP8 L1 L2 N) 512 1109 4436 8.90
(PAIRUP6 L1 L2 N) 512 1296 5184 7.62
(PAIRUP5 L1 L2 N) 512 1765 7060 5.59
(PAIRUP2 L1 L2 N) 512 1861 7444 5.30
(PAIRUP4 L1 L2 N) 512 1890 7560 5.22
(EEA-PAIRUP L1 L2 N) 256 1109 8872 4.45
(PAIRCAB L1 L2 N) 256 1188 9504 4.15
(PAIRS L1 L2 N) 256 1283 10264 3.85
(PAIRUP3 L1 L2 N) 128 1937 30992 1.27
(PAIRUP1 L1 L2 N) 64 1234 39488 1.00
--------------------------------------------------------------------------------
(if (< n (length l))
r
should be(if (< n (length l))
(car r)
(defun ph:make (l n / r f1)
(defun f1(n l) (setq l (reverse l)) (repeat (- (length l) n) (setq l (cdr l)))(reverse l))
(setq r (list (f1 (rem n (length l)) l)))
(apply 'append (repeat (/ n (length l)) (setq r (cons l r)))))