Author Topic: prelst and sufflst - witch code is the best ?  (Read 5712 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
prelst and sufflst - witch code is the best ?
« on: October 16, 2012, 02:39:22 PM »
I can't decide witch code is the best for usage :

Code: [Select]
(setq l '(0 1 2 3 4 5 6 7 8 9))
(prelst l 5) => (0 1 2 3 4)
(sufflst l 5) => (6 7 8 9)

Here they are :
- 1st version
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( lst el / q lstn )
  2.   (foreach e lst
  3.     (if (equal e el 1e-8) (setq q T))
  4.     (if (not q) (setq lstn (cons e lstn)))
  5.   )
  6.   (reverse lstn)
  7. )
  8.  
Code - Auto/Visual Lisp: [Select]
  1. (defun sufflst ( lst el / q lstn )
  2.   (foreach e lst
  3.     (if (equal e el 1e-8) (setq q T))
  4.     (if q (setq lstn (cons e lstn)))
  5.   )
  6.   (cdr (reverse lstn))
  7. )
  8.  
- 2nd version
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( lst el / lstbr lstn )
  2.   (defun lstbr ( lst el )
  3.     (if (not (equal (car lst) el 1e-8))
  4.       (progn
  5.         (setq lstn (cons (car lst) lstn))
  6.         (lstbr (cdr lst) el)
  7.       )
  8.     )
  9.   )
  10.   (lstbr lst el)
  11.   (reverse lstn)
  12. )
  13.  
Code - Auto/Visual Lisp: [Select]
  1. (defun sufflst ( lst el / lstbr q lstn )
  2.   (defun lstbr ( lst el )
  3.     (if (car lst)
  4.       (progn
  5.         (cond ((and (not q) (not (equal (car lst) el 1e-8))) (lstbr (cdr lst) el))
  6.               ((and q (not (equal (car lst) el 1e-8))) (setq lstn (cons (car lst) lstn)) (lstbr (cdr lst) el))
  7.               ((equal (car lst) el 1e-8) (setq q T lstn (cons (car lst) lstn)) (lstbr (cdr lst) el))
  8.         )
  9.       )
  10.     )
  11.   )
  12.   (lstbr lst el)
  13.   (cdr (reverse lstn))
  14. )
  15.  
- 3rd version
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( lst el / q k lstn )
  2.   (setq k -1)
  3.   (while (not q)
  4.     (setq lstn (cons (nth (setq k (1+ k)) lst) lstn))
  5.     (if (equal (nth k lst) el 1e-8) (setq q T))
  6.   )
  7.   (reverse (cdr lstn))
  8. )
  9.  
Code - Auto/Visual Lisp: [Select]
  1. (defun sufflst ( lst el / q k lstn )
  2.   (setq k -1)
  3.   (while (not q)
  4.     (setq k (1+ k))
  5.     (if (equal (nth k lst) el 1e-8) (setq q T lstn (cons (nth k lst) lstn)))
  6.   )
  7.   (while (< k (- (length lst) 1))
  8.     (setq k (1+ k))
  9.     (setq lstn (cons (nth k lst) lstn))
  10.   )
  11.   (cdr (reverse lstn))
  12. )
  13.  

Give me some opinion, or maybe 4th version - I don't know...
Thanks, M.R.

P.S. I don't want to use (member '5 l) instead of (sufflst l 5) - here I have fuzz checking 1e-8...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #1 on: October 16, 2012, 05:15:59 PM »
vl-member-if

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: prelst and sufflst - witch code is the best ?
« Reply #2 on: October 16, 2012, 05:40:59 PM »
Code - Auto/Visual Lisp: [Select]
  1. (setq l '(0 1 2 3 4 5 6 7 8 9))
  2. (reverse (cdr(member 5 (reverse l)))) => (0 1 2 3 4)
  3. (cdr(member 5 l)) => (6 7 8 9)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #3 on: October 16, 2012, 05:45:55 PM »
Code - Auto/Visual Lisp: [Select]
  1. (setq l '(0 1 2 3 4 5 6 7 8 9))
  2. (reverse (cdr(member 5 (reverse l)))) => (0 1 2 3 4)
  3. (cdr(member 5 l)) => (6 7 8 9)

P.S. I don't want to use (member '5 l) instead of (sufflst l 5) - here I have fuzz checking 1e-8...

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: prelst and sufflst - witch code is the best ?
« Reply #4 on: October 16, 2012, 05:46:10 PM »
Code: [Select]
(defun prelst(l i / n r)
  (while (and (setq n (car l))(not(equal n i 1e-8)))
    (setq r (cons n r) l (cdr l)))
  (reverse r)
)
(defun suflst(l i / n r c)
  (setq l (reverse l)  c (length l))
  (while (and (setq n (car l))(not(equal n i 1e-8)))
    (setq r (cons n r) l (cdr l)))
  (if (/= (length r) c) r)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #5 on: October 18, 2012, 12:48:29 PM »
If the List is not the same elements
Code: [Select]
(defun sufflst (p l eps)
  (cond ((not l)  nil)
  ((if (numberp eps) (equal p (car l) eps) (equal p (car l))) (cdr l))
  ((sufflst p (cdr l) eps))
  ))
(defun prelst (p l eps)
  (cond ((not l)  nil)
  ((if (numberp eps) (equal p (car l) eps) (equal p (car l)))  nil)
  ((cons (car l) (prelst p (cdr l) eps))
  )))
Code: [Select]
(preflst 5 '(0 1 2 3 4 5 6 7 8 9 10) 1e-8);;-->'(0 1 2 3 4)
(suffflst 5 '(0 1 2 3 4 5 6 7 8 9 10) 1e-8);;-->'(6 7 8 9 10)
« Last Edit: October 18, 2012, 12:56:52 PM by chlh_jd »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #6 on: October 18, 2012, 01:00:59 PM »
vl-member-if
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l )
  2.     (reverse (cdr (vl-member-if '(lambda ( a ) (equal a x 1e-8)) (reverse l))))
  3. )
  4. (defun suflst ( x l )
  5.     (cdr (vl-member-if '(lambda ( a ) (equal a x 1e-8)) l))
  6. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq l '(0 1 2 3 4 5 6 7 8 9))
  2. (0 1 2 3 4 5 6 7 8 9)
  3. _$ (prelst 5 l)
  4. (0 1 2 3 4)
  5. _$ (suflst 5 l)
  6. (6 7 8 9)

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: prelst and sufflst - witch code is the best ?
« Reply #7 on: October 18, 2012, 01:05:23 PM »
If no duplicates
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:suff (l n)
  2.   (if (equal n (car l) 1e-8) (cdr l) (if l (ph:suff (cdr l) n)))
  3.   )
  4. (defun ph:pref (l n)
  5.   (reverse (ph:suff (reverse l) n))
  6.   )
Code: [Select]
_$ (ph:suff '(0 pi 1.24 a 10 b 11 c r) 10)
(B 11 C R)
_$ (ph:pref '(0 pi 1.24 a 10 b 11 c r) 10)
(0 PI 1.24 A)

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #8 on: October 19, 2012, 04:03:02 AM »
If the List has same elements , Use reverse will get deffrent result .
e.g.
Code: [Select]
'(0 1 2 3 4 5 5 6 7 8 9 10)
Lee's and Stefan's prelst function will get '(0 1 2 3 4 5) , Mine will get '(0 1 2 3 4) , which is Ribarm wanted , Or the List has not same elements ?

I think , if the function used in  Pointset , it perhaps has same points .

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: prelst and sufflst - witch code is the best ?
« Reply #9 on: October 19, 2012, 05:13:45 AM »
Yes, chlh_jd, you're right I was looking for solution with list that may have same elements - like point list thus fuzz 1e-8, and your code is just what I want, but now that I have 4 versions I still can't decide what's the best for usage... Recursion is good but I may encounter hard stack error - or I am wrong - while option is also fast enough, so I implemented CAB's code in his modified by me break object lsp posted on www.cadtutor.net (so that look like it's all CAB's) - fortunately the code concept is that way that it doesn't matter if there are the same points in list so it works... But if I was to ask then my 3rd version with while is near to that what satisfies my needs, and maybe first with foreach if I have to step through all elements in list - seems that this is unavoidable - all list must be processed, because of sufflst in order to imitate real (member or vl-member-if function)... Or maybe combination :

my 3rd version prelst :
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( lst el / q k lstn )
  2.  (setq k -1)
  3.  (while (not q)
  4.    (setq lstn (cons (nth (setq k (1+ k)) lst) lstn))
  5.    (if (equal (nth k lst) el 1e-8) (setq q T))
  6.  )
  7.  (reverse (cdr lstn))
  8. )
  9.  
Lee Mac's version suflst :
Code - Auto/Visual Lisp: [Select]
  1. (defun suflst ( lst el )
  2.    (cdr (vl-member-if '(lambda ( a ) (equal a el 1e-8)) lst))
  3. )
  4.  

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #10 on: October 19, 2012, 05:28:16 AM »
Though Lee Mac suggest method , you can use 'vl-member-if-not function .
Code: [Select]
(defun prelst (x l / n)
  (setq n (length (vl-member-if-not '(lambda (a) (equal a x 1e-8)) l))
l (reverse l))
  (repeat (/ n 4)
    (setq l (cddddr l)))
  (repeat (rem n 4)
    (setq l (cdr l)))
  (reverse l))

Another question is when not match the give atom , return nil or lst ?
Code: [Select]
(ss:prelst 4.5 '(0 1 2 3 4 5 5 6 7 8 9 10) 1e-8);_-->  '(0 1 2 3 4 5 5 6 7 8 9 10)
(LM:prelst 4.5 '(0 1 2 3 4 5 5 6 7 8 9 10));_--> NIL
which result do you want ?
« Last Edit: October 19, 2012, 05:37:08 AM by chlh_jd »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #11 on: October 19, 2012, 05:36:57 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l / f )
  2.     (vl-remove-if '(lambda ( a ) (or f (setq f (equal a x 1e-8)))) l)
  3. )
Code - Auto/Visual Lisp: [Select]
  1. (defun suflst ( x l / f )
  2.     (cdr (vl-remove-if-not '(lambda ( a ) (or f (setq f (equal x a 1e-8)))) l))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l )
  2.     (if (and l (not (equal x (car l) 1e-8)))
  3.         (cons (car l) (prelst x (cdr l)))
  4.     )
  5. )
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l / r )
  2.     (vl-some '(lambda ( a ) (or (equal x a 1e-8) (not (setq r (cons a r))))) l)
  3.     (reverse r)
  4. )
« Last Edit: October 19, 2012, 06:48:11 AM by Lee Mac »

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #12 on: October 19, 2012, 05:42:04 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l / f )
  2.     (vl-remove-if '(lambda ( a ) (or f (setq f (equal a x 1e-8)))) l)
  3. )
Nice code !  Lee  :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #13 on: October 19, 2012, 05:50:11 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l / f )
  2.     (vl-remove-if '(lambda ( a ) (or f (setq f (equal a x 1e-8)))) l)
  3. )
Nice code !  Lee  :-)

Thank you!  :-)

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #14 on: October 19, 2012, 07:09:35 AM »
my old version 'eqmember' Compare , it has test by numberlist and pointset . now add Lee Mac's suflst , and just test in my win7 64Bit Acad2011 version VLIDE.
Code: [Select]
(defun f1 (p l eps)
  (cond ((not l)  nil)
  ((if (numberp eps) (equal p (car l) eps) (equal p (car l)))  l)
  ((f1 p (cdr l) eps))
  ))
(defun f2  (a l eps / r b)
  (while(and(not r)l)
    (setq b(car l)l(cdr l))
    (if(if(numberp eps)(equal a b eps)(equal a b))(setq r t)))
  (if r(cons b l)))
(defun f3  (a l eps)
  (member (car (vl-remove-if-not
(function (lambda (x)
     (if (numberp eps)
       (equal a x eps)
       (equal a x))))
l)) l))
(defun Lm:suflst (x l eps) (vl-member-if '(lambda (a) (equal a x eps)) l))
Test function 'Quickbench see here  http://www.theswamp.org/index.php?action=dlattach;topic=42091.0;attach=22832
Code: [Select]
(QuickBench (mapcar '(lambda (f) (list f '5 ''(0 1 2 3 5 4 5 5 6 7 8 9 10) '1e-8)) '(f1 f2 f3 lm:suflst)))
my test result
Code: [Select]

_$
Benchmarking .... done for 8192 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(F1 5 (QUOTE (0 1 2 3 5 4 5 5 6 7 8 ...)      8192      1123      1123      3.11
(F2 5 (QUOTE (0 1 2 3 5 4 5 5 6 7 8 ...)      8192      1155      1155      3.02
(F3 5 (QUOTE (0 1 2 3 5 4 5 5 6 7 8 ...)      8192      1170      1170      2.98
(LM:SUFLST 5 (QUOTE (0 1 2 3 5 4 5 5...)      4096      1746      3492      1.00
--------------------------------------------------------------------------------
_$

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #15 on: October 19, 2012, 07:22:54 AM »
In a hurry to go out, have not had time to join the others function to compare ,
I am very apologize . good nithgt !

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #16 on: October 19, 2012, 07:35:05 AM »
Code: [Select]
(defun prelst (x l / n)
  (setq n (length (vl-member-if-not '(lambda (a) (equal a x 1e-8)) l))
l (reverse l))
  (repeat (/ n 4)
    (setq l (cddddr l)))
  (repeat (rem n 4)
    (setq l (cdr l)))
  (reverse l))

This will always return nil if the supplied item is not the first element of the list (since vl-member-if-not will return the whole list if not).

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #17 on: October 19, 2012, 07:41:18 AM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l / f )
  2.     (defun f ( x l a )
  3.         (if l
  4.             (if (equal x (car l) 1e-8)
  5.                 (reverse a)
  6.                 (f x (cdr l) (cons (car l) a))
  7.             )
  8.             (reverse a)
  9.         )
  10.     )
  11.     (f x l nil)
  12. )
« Last Edit: October 19, 2012, 07:45:54 AM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #18 on: October 19, 2012, 07:52:59 AM »
Benchmarking all prelst functions:

Test Function:
Code - Auto/Visual Lisp: [Select]
  1. ;; Benchmark function by MP
  2. ;; http://www.theswamp.org/lilly_pond/mp/lisp/benchmark.txt
  3.  
  4. (defun c:test ( / f i l n )
  5.     (setq i 0)
  6.     (repeat 100 (setq l (cons (setq i (1+ i)) l)))
  7.     (setq l (reverse l))
  8.     (setq f
  9.        '(
  10.             (mr:prelst1 l n)
  11.             (mr:prelst2 l n)
  12.             (mr:prelst3 l n)
  13.             (cab:prelst l n)
  14.             (ch:prelst  n l 1e-8)
  15.             (lm:prelst1 n l)
  16.             (ph:pref    l n)
  17.             (lm:prelst2 n l)
  18.             (lm:prelst3 n l)
  19.             (lm:prelst4 n l)
  20.             (lm:prelst5 n l)
  21.         )
  22.     )
  23.  
  24.     (setq n 25)
  25.     (benchmark f)
  26.     (gc)
  27.     (setq n 50)
  28.     (benchmark f)
  29.     (gc)
  30.     (setq n 75)
  31.     (benchmark f)
  32.     (gc)
  33.     (princ)
  34. )

List of 100 items, return portion of list up to item n:

n=25
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):
  2.  
  3.     (CAB:PRELST L N).............1014 / 2.28 <fastest>
  4.     (LM:PRELST3 N L).............1076 / 2.15
  5.     (LM:PRELST5 N L).............1154 / 2.00
  6.     (MR:PRELST2 L N).............1217 / 1.90
  7.     (CH:PRELST N L 1.0e-008).....1264 / 1.83
  8.     (MR:PRELST3 L N).............1295 / 1.78
  9.     (LM:PRELST1 N L).............1778 / 1.30
  10.     (PH:PREF L N)................1825 / 1.27
  11.     (LM:PRELST2 N L).............1825 / 1.27
  12.     (LM:PRELST4 N L).............1903 / 1.21
  13.     (MR:PRELST1 L N).............2309 / 1.00 <slowest>

n=50
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):
  2.  
  3.     (PH:PREF L N)................1435 / 1.70 <fastest>
  4.     (CAB:PRELST L N).............1497 / 1.63
  5.     (LM:PRELST3 N L).............1654 / 1.47
  6.     (LM:PRELST5 N L).............1748 / 1.39
  7.     (LM:PRELST1 N L).............1763 / 1.38
  8.     (MR:PRELST2 L N).............1856 / 1.31
  9.     (LM:PRELST2 N L).............1919 / 1.27
  10.     (CH:PRELST N L 1.0e-008).....1981 / 1.23
  11.     (MR:PRELST3 L N).............2184 / 1.11
  12.     (LM:PRELST4 N L).............2262 / 1.08
  13.     (MR:PRELST1 L N).............2434 / 1.00 <slowest>

n=75
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):
  2.  
  3.     (PH:PREF L N)................1030 / 3.07 <fastest>
  4.     (LM:PRELST1 N L).............1528 / 2.07
  5.     (LM:PRELST2 N L).............1997 / 1.59
  6.     (CAB:PRELST L N).............2028 / 1.56
  7.     (LM:PRELST3 N L).............2277 / 1.39
  8.     (LM:PRELST5 N L).............2356 / 1.34
  9.     (MR:PRELST2 L N).............2496 / 1.27
  10.     (MR:PRELST1 L N).............2590 / 1.22
  11.     (LM:PRELST4 N L).............2605 / 1.22
  12.     (CH:PRELST N L 1.0e-008).....2777 / 1.14
  13.     (MR:PRELST3 L N).............3167 / 1.00 <slowest>

Attached are the tested functions.
« Last Edit: October 19, 2012, 07:56:27 AM by Lee Mac »

CADDOG

  • Newt
  • Posts: 82
  • wishbonesr
Re: prelst and sufflst - witch code is the best ?
« Reply #19 on: October 19, 2012, 03:26:50 PM »
Benchmarking all prelst functions:

The official ruling has been challenged.  :police:
Changing quoted lambda's to optimized (functions) produced a different photo finish.

EDIT:
Lee, I did not raise the ceiling, but did so after you pointed it out.

I removed my results (because of it's innacuracy), and

The top competitors in my own tests were quite supprising.  This is probably due to the lack of overhead that's required for iterative methods.
I was able to get 10,001 length lists to run for all functions..
The tests are fantastic and show me that optimization is important, but also that the least verbose isn't always the bestest under heavy loads of bulk data processing.

Overhead of a function also doesn't mean it's the worst either.  Evidence by the likes of the MergeSort algorithm, which uses three times the memory as, say, a JSort or InsertSort.  At one million entries MergeSort excels while Jsort kills MergeSort below 50,000 (of course this depends further on your implementation methods and even comilers).

The different bench test results vary from the method of testing and the data set as we've seen through the multiple user results.  This thread has shown me that your data set just needs to be considered and the generous contributions tested yourself.

Here's the suprising results I got out of 10,001 length list, contradicts other user's results.  I did not expect function optimized lambda's to perform so poorly.  I remember a post from long ago that explained (in autolisp) why it's one way or the other, but I remember neither the lesson of the author nor where I read it.
Code - Auto/Visual Lisp: [Select]
  1. n=10,001
  2. Elapsed milliseconds / relative speed for 32768 iteration(s):
  3.     (LM:PRELST3 N L)...............1217 / 684.78 <fastest>
  4.     (CAB:PRELST L N)...............1232 / 676.44
  5.     (CH:PRELST N L 1.0e-008).......1295 / 643.53
  6.     (LM:PRELST5 N L)...............1295 / 643.53
  7.     (MR:PRELST2 L N)...............1310 / 636.16
  8.     (MR:PRELST3 L N)...............1326 / 628.49
  9.     (LM:PRELST4 N L)...............1544 / 539.75
  10.     (LM:PRELST2 N L).............180992 / 4.60
  11.     (LM:PRELST1 N L).............343389 / 2.43
  12.     (PH:PREF L N)................825308 / 1.01
  13.     (MR:PRELST1 L N).............833373 / 1.00 <slowest>
  14.  

Under a presumption that garabage collection could be skewing the results of functions that were later in the test, I switched the order of execution for the test, and the same results were obtained.

As far as I can see ((now)), that there is not just one winner, rather there were 7.  I've enjoyed this thread emensly.  Looking forward to see if there's' any basis of what I noticed with lambda.

They were compiled.
« Last Edit: October 21, 2012, 09:01:37 PM by CADDOG »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #20 on: October 19, 2012, 04:49:15 PM »
The official ruling has been challenged.  :police:
Changing quoted lambda's to optimized (functions) produced a different photo finish.
LM:PRELST1 is the clear winner for most situations.
Code - Auto/Visual Lisp: [Select]
  1. (defun lm:prelst1 ( x l )
  2.     (reverse (cdr (vl-member-if (function (lambda ( a ) (equal a x 1e-8))) (reverse l))))
  3. )

Good spot CADDOG, thanks :-)

Code - Auto/Visual Lisp: [Select]
  1. n=150
  2.  
  3. < ... >
  4.  
  5. n=5,000
  6.  
  7. < ... >
  8.  
  9. n=10,000

Did you ensure that you altered the length of the list l for these tests involving higher values of the parameter n? Since any value of n over 100 for the curret list size would yield the same result  :wink:

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: prelst and sufflst - witch code is the best ?
« Reply #21 on: October 20, 2012, 03:28:43 AM »
Maybe it's the fastest, but what you haven't checked - it's also wrong one :

Code: [Select]
Command: (setq l '(0 1 2 3 4 5 5 5 6 7 8 9))
(0 1 2 3 4 5 5 5 6 7 8 9)

Command: (lm:prelst1 5 l)
(0 1 2 3 4 5 5)

prelst function should return all first elements from list until first occur of supplied element :
Code: [Select]
Command: (setq l '(0 1 2 3 4 5 5 5 6 7 8 9))
(0 1 2 3 4 5 5 5 6 7 8 9)

Command: (prelst 5 l)
(0 1 2 3 4)

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #22 on: October 21, 2012, 12:12:34 AM »
Code: [Select]
(defun prelst (x l / n)
  (setq n (length (vl-member-if-not '(lambda (a) (equal a x 1e-8)) l))
l (reverse l))
  (repeat (/ n 4)
    (setq l (cddddr l)))
  (repeat (rem n 4)
    (setq l (cdr l)))
  (reverse l))

This will always return nil if the supplied item is not the first element of the list (since vl-member-if-not will return the whole list if not).
It did return NIL .
That day has no time test .

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #23 on: October 21, 2012, 12:13:38 AM »
What about test with Big Pointset ?
More Point can use Qjchen's post . http://www.theswamp.org/index.php?topic=32874.0
Or see I post the attachment
« Last Edit: October 21, 2012, 12:33:09 AM by chlh_jd »

pBe

  • Bull Frog
  • Posts: 402
Re: prelst and sufflst - witch code is the best ?
« Reply #24 on: October 21, 2012, 12:57:04 AM »
wouldn't it better to use one function for both.

example
Code - Auto/Visual Lisp: [Select]
  1. (defun leavem (n l m / x)
  2.         (if (setq x (vl-remove-if (function (lambda (j)
  3.                         (m n j))) l)) x l))

(setq ls '(0 1 2 3 4 5 5 6 7 8 9 10) )

 (leavem 5 ls >=)
(6 7 8 9 10)

 (leavem 5 ls <=)
(0 1 2 3 4)

(leavem 25 ls <=)
(0 1 2 3 4 5 5 6 7 8 9 10)

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #25 on: October 21, 2012, 01:11:30 AM »
 
What about test with Big Pointset ?
More Point can use Qjchen's post . http://www.theswamp.org/index.php?topic=32874.0
Or see I post the attachment
Test in this post dwg , 10000 random points (No same points) .
The benchmark function can't get the test result , error massage :
Code: [Select]
... hard errors ***
Has reached an internal stack limit (analog)
So I change into quickbench .
Quickbench see here http://www.theswamp.org/index.php?action=dlattach;topic=42091.0;attach=22832
Test result with Lee Mac's compare lsp , radom select the search point .
1st (Select center point)
Code: [Select]
Benchmarking ......... done for 256 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(LM:PRELST2 N L)                               256      1576      1576    153.34
(PH:PREF N L)                                  256      1717      1717    140.75
(LM:PRELST4 N L)                               128      1184      2368    102.05
(LM:PRELST3 N L)                               128      1732      3464     69.76
(LM:PRELST5 N L)                               128      1809      3618     66.79
(CH:PRELST N L)                                128      1810      3620     66.76
(MR:PRELST2 N L)                                64      1484      5936     40.71
(MR:PRELST1 N L)                                64      1764      7056     34.25
(MR:PRELST3 N L)                                 2      1888    241664      1.00
--------------------------------------------------------------------------------
2nd (Select rightbottom point)
Code: [Select]
Benchmarking ......... done for 512 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(LM:PRELST4 N L)                               512      1044      1044     13.16
(LM:PRELST3 N L)                               512      1513      1513      9.08
(LM:PRELST5 N L)                               512      1532      1532      8.97
(CH:PRELST N L)                                512      1545      1545      8.89
(LM:PRELST2 N L)                               512      1825      1825      7.53
(MR:PRELST2 N L)                               256      1357      2714      5.06
(PH:PREF N L)                                  128      1591      6364      2.16
(MR:PRELST1 N L)                                64      1419     11352      1.21
(MR:PRELST3 N L)                                64      1717     13736      1.00
--------------------------------------------------------------------------------
3rd (Select LeftTop point)
Code: [Select]
Benchmarking ......... done for 256 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(PH:PREF N L)                                  256      1421      1421    233.30
(LM:PRELST2 N L)                               256      1686      1686    196.63
(LM:PRELST4 N L)                               128      1357      2714    122.15
(LM:PRELST3 N L)                               128      2013      4026     82.34
(LM:PRELST5 N L)                                64      1015      4060     81.66
(CH:PRELST N L)                                 64      1029      4116     80.54
(MR:PRELST1 N L)                                32      1093      8744     37.91
(MR:PRELST2 N L)                                32      1137      9096     36.45
(MR:PRELST3 N L)                                 2      2590    331520      1.00
--------------------------------------------------------------------------------
4th (Select LeftBottom point)
Code: [Select]
Benchmarking ......... done for 128
iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(LM:PRELST2 N L)                               128      1949      1949     32.26
(LM:PRELST4 N L)                               128      2012      2012     31.25
(PH:PREF N L)                                   64      1590      3180     19.77
(LM:PRELST5 N L)                                64      1904      3808     16.51
(LM:PRELST3 N L)                                64      2012      4024     15.63
(MR:PRELST2 N L)                                32      1047      4188     15.01
(CH:PRELST N L)                                 32      1169      4676     13.45
(MR:PRELST1 N L)                                32      1545      6180     10.17
(MR:PRELST3 N L)                                 4      1965     62880      1.00
--------------------------------------------------------------------------------
5th (Select RightTop point)
Code: [Select]
Benchmarking ......... done for 256
iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(PH:PREF N L)                                  256      1092      1092    267.02
(LM:PRELST2 N L)                               256      1856      1856    157.10
(LM:PRELST4 N L)                               128      1514      3028     96.30
(CH:PRELST N L)                                 64      1170      4680     62.30
(LM:PRELST5 N L)                                64      1172      4688     62.20
(LM:PRELST3 N L)                                64      1201      4804     60.70
(MR:PRELST1 N L)                                32      1093      8744     33.35
(MR:PRELST2 N L)                                32      1357     10856     26.86
(MR:PRELST3 N L)                                 1      1139    291584      1.00
--------------------------------------------------------------------------------
« Last Edit: October 21, 2012, 01:34:43 AM by chlh_jd »

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #26 on: October 21, 2012, 01:51:10 AM »
Above compares are not compiled , It show the LM:prelst2 win it .
Code: [Select]
;; Lee Mac
;; http://www.theswamp.org/index.php?topic=43004.msg482478#msg482478

(defun lm:prelst2 ( x l / f )
    (vl-remove-if '(lambda ( a ) (or f (setq f (equal a x 1e-8)))) l)
)

Just a little doubt : 'vl-remove-if must traverse the entire List , after find it omitted 'equal .

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #27 on: October 21, 2012, 05:02:01 AM »
The above missed Alan function, I so apologize .
Retest after compiled , it get diffrrent result
1st (Center point)
Code: [Select]
Benchmarking .......... done for 256 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(PH:PREF N L)                                  256      1325      1325    153.70
(MR:PRELST1 N L)                               128      1013      2026    100.52
(CAB:PRELST N L)                               128      1014      2028    100.42
(LM:PRELST5 N L)                               128      1232      2464     82.65
(CH:PRELST N L)                                128      1265      2530     80.49
(LM:PRELST3 N L)                               128      1265      2530     80.49
(MR:PRELST2 N L)                               128      1341      2682     75.93
(LM:PRELST2 N L)                               128      1450      2900     70.22
(LM:PRELST4 N L)                               128      1810      3620     56.26
(MR:PRELST3 N L)                                 2      1591    203648      1.00
--------------------------------------------------------------------------------
2nd (RB point)
Code: [Select]
Benchmarking .......... done for 512 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(CAB:PRELST N L)                               512      1966      1966     40.11
(LM:PRELST5 N L)                               256      1217      2434     32.39
(LM:PRELST3 N L)                               256      1218      2436     32.37
(CH:PRELST N L)                                256      1232      2464     32.00
(MR:PRELST2 N L)                               256      1295      2590     30.44
(MR:PRELST1 N L)                               256      1638      3276     24.07
(LM:PRELST4 N L)                               256      1919      3838     20.54
(PH:PREF N L)                                  128      1030      4120     19.14
(LM:PRELST2 N L)                               128      1248      4992     15.79
(MR:PRELST3 N L)                                 8      1232     78848      1.00
--------------------------------------------------------------------------------
3rd (LT point)
Code: [Select]
Benchmarking .......... done for 512 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(PH:PREF N L)                                  512      1637      1637    458.67
(MR:PRELST1 N L)                               128      1173      4692    160.03
(CAB:PRELST N L)                               128      1419      5676    132.28
(LM:PRELST5 N L)                               128      1715      6860    109.45
(CH:PRELST N L)                                128      1732      6928    108.38
(LM:PRELST3 N L)                               128      1746      6984    107.51
(MR:PRELST2 N L)                               128      1903      7612     98.64
(LM:PRELST2 N L)                                64      1015      8120     92.47
(LM:PRELST4 N L)                                64      1482     11856     63.33
(MR:PRELST3 N L)                                 2      2933    750848      1.00
--------------------------------------------------------------------------------
4th (LB point)
Code: [Select]
Benchmarking .......... done for 512 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(PH:PREF N L)                                  512      1436      1436    372.59
(MR:PRELST1 N L)                               128      1216      4864    110.00
(CAB:PRELST N L)                               128      1467      5868     91.18
(CH:PRELST N L)                                128      1826      7304     73.25
(LM:PRELST3 N L)                               128      1857      7428     72.03
(LM:PRELST5 N L)                               128      1857      7428     72.03
(MR:PRELST2 N L)                               128      1996      7984     67.01
(LM:PRELST2 N L)                                64      1140      9120     58.67
(LM:PRELST4 N L)                                64      1637     13096     40.86
(MR:PRELST3 N L)                                 1      1045    535040      1.00
--------------------------------------------------------------------------------
5th (RT pint)
Code: [Select]
Benchmarking .......... done for 256
iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(CAB:PRELST N L)                               256      1562      1562     81.17
(PH:PREF N L)                                  256      1652      1652     76.75
(MR:PRELST1 N L)                               256      1902      1902     66.66
(LM:PRELST5 N L)                               256      1903      1903     66.62
(CH:PRELST N L)                                256      1966      1966     64.49
(LM:PRELST3 N L)                               256      1981      1981     64.00
(MR:PRELST2 N L)                               128      1044      2088     60.72
(LM:PRELST2 N L)                               128      1326      2652     47.81
(LM:PRELST4 N L)                               128      1450      2900     43.72
(MR:PRELST3 N L)                                 4      1981    126784      1.00
--------------------------------------------------------------------------------
« Last Edit: October 21, 2012, 05:14:08 AM by chlh_jd »

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #28 on: October 21, 2012, 05:21:17 AM »
It seems PH:PREF is the winner .
But  in the same points case , PH:PREF's result is not M.Ribar wanted ;and in the no element case, it return nil .

It alos show us 'foreach function can be multi-threaded computing  after compiled  ?
(My computer and system : Core i7 920 @ 2.67GHz , 16.0 G Mem , Win7 64 bit , ACAD 2011 .)

« Last Edit: October 21, 2012, 05:29:43 AM by chlh_jd »