Author Topic: Looking for a better way...  (Read 8021 times)

0 Members and 1 Guest are viewing this topic.

Stefan

  • Bull Frog
  • Posts: 235
Re: Looking for a better way...
« Reply #45 on: July 02, 2012, 04:53:13 AM »
I thought this topic is closed.
But if it was revived, I added another method for make-list.

Code - Auto/Visual Lisp: [Select]
  1. (defun ph:make (l n / r)
  2.   (setq r (list (mapcar '(lambda (x y) x) l (repeat (rem n (length l)) (setq r (cons 1 r))))))
  3.      (repeat (/ n (length l)) (setq r (cons l r)))
  4.     )
  5.   )
However, I think the result can be improved if some other method is used instead of (repeat.
Pairs1 and Pairs2 code inside of test lisp.
Here are the results:

Code: [Select]
_$ (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
--------------------------------------------------------------------------------

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Looking for a better way...
« Reply #46 on: July 02, 2012, 05:26:41 AM »
Awesome! That one's actually a lot nicer ... and faster!

Although, just be careful of running into the same trap I've run into before:
Code - Auto/Visual Lisp: [Select]
  1. _$ (ph:make '(1 2 3 4 5 6 7) 5)
  2. nil
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: Looking for a better way...
« Reply #47 on: July 02, 2012, 05:34:11 AM »
Hope you don't mind ... I've altered the make-list-from:
Code - Auto/Visual Lisp: [Select]
  1. (defun make-list-from  (n l / r)
  2.   (if (= n (length l))
  3.     l
  4.     (progn
  5.       (setq r (list (mapcar '(lambda (x y) x) l (repeat (rem n (length l)) (setq r (cons 1 r))))))
  6.       (if (< n (length l))
  7.         r
  8.         (apply 'append (repeat (/ n (length l)) (setq r (cons l r))))))))
Now the benchmarking's a bit different:
Code - Auto/Visual Lisp: [Select]
  1. _$ (PairupTest)
  2.  
  3. Testing with int list of 48 length, and counter of 814
  4. --------------------------------------------------------------------------------
  5. Testing for correctness in relation to the PAIRS function:
  6. PAIRUP1 = T
  7. PAIRUP2 = T
  8. PAIRUP3 = T
  9. PAIRUP4 = T
  10. PAIRUP5 = T
  11. PAIRCAB = nil
  12. PAIRUP6 = T
  13. PAIRUP7 = T
  14. PAIRUP8 = T
  15. PAIRUP9 = T
  16. PAIRLISTS = T
  17. PAIRLISTS2 = T
  18. PAIRLISTS3 = T
  19. EEA-PAIRUP = T
  20. PAIRUP10 = T
  21. PAIRS1 = T
  22. PAIRS2 = T
  23.  
  24. Benchmarking .................. done for 2048 iterations. Sorted from fastest.
  25. Statement                                Increment  Time(ms) Normalize  Relative
  26. --------------------------------------------------------------------------------
  27. (PAIRS1 L1 L2 N)                              2048      1435      1435     21.92
  28. (PAIRLISTS3 L1 L2 N)                          2048      1685      1685     18.67
  29. (PAIRS2 L1 L2 N)                              2048      1686      1686     18.66
  30. (PAIRUP9 L1 L2 N)                             2048      1777      1777     17.70
  31. (PAIRUP10 L1 L2 N)                            2048      1841      1841     17.09
  32. (PAIRUP8 L1 L2 N)                              512      1076      4304      7.31
  33. (PAIRUP7 L1 L2 N)                              512      1077      4308      7.30
  34. (PAIRLISTS L1 L2 N)                            512      1092      4368      7.20
  35. (PAIRLISTS2 L1 L2 N)                           512      1092      4368      7.20
  36. (PAIRUP6 L1 L2 N)                              512      1435      5740      5.48
  37. (PAIRUP4 L1 L2 N)                              512      1966      7864      4.00
  38. (PAIRUP5 L1 L2 N)                              512      2027      8108      3.88
  39. (PAIRS L1 L2 N)                                256      1045      8360      3.76
  40. (PAIRCAB L1 L2 N)                              256      1045      8360      3.76
  41. (PAIRUP2 L1 L2 N)                              256      1047      8376      3.76
  42. (EEA-PAIRUP L1 L2 N)                           256      1139      9112      3.45
  43. (PAIRUP3 L1 L2 N)                              128      1451     23216      1.35
  44. (PAIRUP1 L1 L2 N)                              128      1966     31456      1.00
  45. --------------------------------------------------------------------------------
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Stefan

  • Bull Frog
  • Posts: 235
Re: Looking for a better way...
« Reply #48 on: July 02, 2012, 06:07:51 AM »
OMG you're right.
This
Code: [Select]
(if (< n (length l))
       r
should be
Code: [Select]
(if (< n (length l))
       (car r)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Looking for a better way...
« Reply #49 on: July 02, 2012, 06:15:56 AM »
Thanks yes, I should've noticed  :-[
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

chlh_jd

  • Guest
Re: Looking for a better way...
« Reply #50 on: July 09, 2012, 10:19:08 AM »
For short list perhaps  :lol:
Code: [Select]
(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)))))