Author Topic: null string in list  (Read 4771 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12313
  • London, England
Re: null string in list
« Reply #30 on: August 04, 2015, 12:24:38 PM »
Oh, I made it smaller!? This one only checks to see if all entries are the same though.

Code - Auto/Visual Lisp: [Select]
  1. (apply '= (mapcar 'ascii '("" "" "")))

A slight tweak would suit the task  :wink:
Code - Auto/Visual Lisp: [Select]
  1. (vl-every 'zerop (mapcar 'ascii '("" "" "")))

Lee Mac

  • Seagull
  • Posts: 12313
  • London, England
Re: null string in list
« Reply #31 on: August 04, 2015, 01:30:52 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun f4 ( l )
  2.     (not (while (= "" (car l)) (setq l (cdr l))))
  3. )

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1025
  • Marco
Re: null string in list
« Reply #32 on: August 04, 2015, 04:32:35 PM »
slover:
Code: [Select]
(defun f5 (l) (if (= "" (car l)) (= "" (last (vl-sort l '=)))))
Code: [Select]
(setq alist '("" "" "" ""))
Benchmark.lsp | 2005 Michael Puckett | All Rights Reserved
Elapsed milliseconds / relative speed for 262144 iteration(s):
    (VL-REMOVE1 ALIST).....1719 / 1.29 <fastest>
    (VL-REMOVE1 ALIST).....1734 / 1.28
    (F4 ALIST).............1938 / 1.14
    (F4 ALIST).............1969 / 1.13
    (VL-EVERY12 ALIST).....2015 / 1.1
    (VL-EVERY12 ALIST).....2016 / 1.1
    (APPLY1 ALIST).........2047 / 1.08
    (APPLY1 ALIST).........2047 / 1.08
    (F ALIST)..............2093 / 1.06
    (F ALIST)..............2125 / 1.04
    (F5 ALIST).............2125 / 1.04
    (APPLY3 ALIST).........2125 / 1.04
    (F5 ALIST).............2125 / 1.04
    (APPLY3 ALIST).........2141 / 1.04
    (APPLY2 ALIST).........2203 / 1.01
    (APPLY2 ALIST).........2219 / 1 <slowest>
Elapsed milliseconds / relative speed for 262144 iteration(s):
    (VL-REMOVE1 ALIST).....1765 / 1.31 <fastest>
    (VL-REMOVE1 ALIST).....1766 / 1.31
    (F4 ALIST).............1969 / 1.17
    (VL-EVERY12 ALIST).....2031 / 1.14
    (F4 ALIST).............2046 / 1.13
    (VL-EVERY12 ALIST).....2047 / 1.13
    (APPLY1 ALIST).........2109 / 1.1
    (APPLY1 ALIST).........2110 / 1.1
    (F ALIST)..............2140 / 1.08
    (F5 ALIST).............2171 / 1.07
    (F ALIST)..............2188 / 1.06
    (F5 ALIST).............2188 / 1.06
    (APPLY3 ALIST).........2188 / 1.06
    (APPLY3 ALIST).........2203 / 1.05
    (APPLY2 ALIST).........2265 / 1.02
    (APPLY2 ALIST).........2313 / 1 <slowest>

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1025
  • Marco
Re: null string in list
« Reply #33 on: August 04, 2015, 05:21:33 PM »
Code: [Select]
(defun f6 (l) (if (= "" (car l)) (not (while (vl-position "" (setq l (cdr l)))))))
Code: [Select]
Elapsed milliseconds / relative speed for 262144 iteration(s):
    (VL-REMOVE1 ALIST2).....1703 / 1.29 <fastest>
    (F6 ALIST2).............1875 / 1.17
    (F4 ALIST2).............1953 / 1.13
    (VL-EVERY12 ALIST2).....1969 / 1.12
    (APPLY1 ALIST2).........2078 / 1.06
    (APPLY3 ALIST2).........2140 / 1.03
    (APPLY2 ALIST2).........2203 / 1 <slowest>

Elapsed milliseconds / relative speed for 262144 iteration(s):
    (VL-REMOVE1 ALIST2)......1688 / 8.65 <fastest>
    (F6 ALIST2)..............1875 / 7.79
    (F4 ALIST2)..............1938 / 7.54
    (APPLY1 ALIST2)..........2031 / 7.19
    (APPLY2 ALIST2)..........2187 / 6.68
    (APPLY3 ALIST2).........12219 / 1.2
    (VL-EVERY12 ALIST2).....14609 / 1 <slowest>
Strange result on  VL-EVERY12 and APPLY3 on second test.
   

Lee Mac

  • Seagull
  • Posts: 12313
  • London, England
Re: null string in list
« Reply #34 on: August 04, 2015, 05:27:29 PM »
Code - Auto/Visual Lisp: [Select]
  1. _$ (f6 '("" "" "a"))
  2. T

;)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1025
  • Marco
Re: null string in list
« Reply #35 on: August 04, 2015, 05:29:15 PM »
@Lee ok I take a look...

long list:
Code: [Select]
(setq alist '("" "" "" ""))
(setq alist2  alist)
(setq alist2 (repeat 200 (setq alist2 (append alist2 alist))))
Code: [Select]
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (VL-REMOVE1 ALIST2)......1172 / 10.95 <fastest>
    (APPLY1 ALIST2)..........2328 / 5.51
    (VL-EVERY12 ALIST2)......4875 / 2.63
    (APPLY3 ALIST2)..........6094 / 2.11
    (APPLY2 ALIST2)..........6125 / 2.09
    (F6 ALIST2)..............8469 / 1.51
    (F4 ALIST2).............12828 / 1 <slowest>

Elapsed milliseconds / relative speed for 65536 iteration(s):
    (VL-REMOVE1 ALIST2)......1031 / 11.77 <fastest>
    (APPLY1 ALIST2)..........2094 / 5.8
    (VL-EVERY12 ALIST2)......4907 / 2.47
    (APPLY2 ALIST2)..........6094 / 1.99
    (APPLY3 ALIST2)..........6203 / 1.96
    (F6 ALIST2)..............8188 / 1.48
    (F4 ALIST2).............12140 / 1 <slowest>

Lee Mac

  • Seagull
  • Posts: 12313
  • London, England
Re: null string in list
« Reply #36 on: August 04, 2015, 05:45:36 PM »
Don't think we've had this one yet:
Code - Auto/Visual Lisp: [Select]
  1. (defun f7 ( x ) (eval (vl-list* '= "" x)))

bruno_vdh

  • Guest
Re: null string in list
« Reply #37 on: August 04, 2015, 06:21:19 PM »
Don't think we've had this one yet:
Code - Auto/Visual Lisp: [Select]
  1. (defun f7 ( x ) (eval (vl-list* '= "" x)))

I have preferred him apply because eval evaluates arguments
Code: [Select]
_$ (setq A "")
""
_$ (= "" 'A 'A 'A)
nil
_$ (apply '= '("" A A A))
nil
_$ (eval  '(= "" A A A))
T

Regards,

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1025
  • Marco
Re: null string in list
« Reply #38 on: August 05, 2015, 03:27:29 AM »
Code: [Select]
(defun f6 (l) ;F4 like
  (if (= "" (car l))
    (while (= "" (car (setq l (cdr l)))))
  )
  (not l)
)
Code: [Select]
(setq alist '("" "" "" ""))
(setq alist2  alist)
(setq alist2 (repeat 20 (setq alist2 (append alist2 alist))))
Elapsed milliseconds / relative speed for 131072 iteration(s):
    (VL-REMOVE1 ALIST2).....1000 / 5.44 <fastest>
    (APPLY1 ALIST2).........1390 / 3.91
    (VL-EVERY12 ALIST2).....1875 / 2.9
    (APPLY3 ALIST2).........2093 / 2.6
    (APPLY2 ALIST2).........2157 / 2.52
    (F6 ALIST2).............3281 / 1.66
    (F4 ALIST2).............3406 / 1.6
    (F7 ALIST2).............5438 / 1 <slowest>
   
 
Elapsed milliseconds / relative speed for 131072 iteration(s):
    (VL-REMOVE1 ALIST2).....1047 / 5.3 <fastest>
    (APPLY1 ALIST2).........1468 / 3.78
    (VL-EVERY12 ALIST2).....1907 / 2.91
    (APPLY3 ALIST2).........2250 / 2.47
    (APPLY2 ALIST2).........2281 / 2.43
    (F6 ALIST2).............3422 / 1.62
    (F4 ALIST2).............3438 / 1.61
    (F7 ALIST2).............5547 / 1 <slowest>

roy_043

  • Water Moccasin
  • Posts: 1747
  • BricsCAD 18
Re: null string in list
« Reply #39 on: August 05, 2015, 04:28:12 AM »
@ Marc'Antonio:
Your speed test is interesting but you only test with a list in which all elements are equal. If there is an alternate element halfway in the list the results will be quit different. For example, as bruno_vdh already hinted, solutions using (vl-every) will then perform relatively better.

bruno_vdh

  • Guest
Re: null string in list
« Reply #40 on: August 05, 2015, 06:17:26 AM »
Good explanation of roy_043, if one is maniac in comparisons, we can add a condition to apply and every for a nil argument  :wink:
Code: [Select]
(and lst (vl-every '= (cons "" lst) lst))
(and lst (apply '= (cons "" lst)))

For the game, one last less powerful variant with vl-member-if-not
Code: [Select]
(defun f (l)
   (if l
     (not (vl-member-if-not '(lambda (x) (eq "" x)) l))
   )
 )

Regards,
« Last Edit: August 05, 2015, 06:25:04 AM by bruno_vdh »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1025
  • Marco
Re: null string in list
« Reply #41 on: August 05, 2015, 08:37:30 AM »
Ok... new test:
Code: [Select]
(setq alist0 '(""  "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""))
(setq alistS '("1" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""))
(setq alistE '("" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "1"))

Elapsed milliseconds / relative speed for 262144 iteration(s):
    (VL-REMOVE1 ALIST0).....1828 / 3.8 <fastest>
    (APPLY1 ALIST0).........2312 / 3.01
    (VL-EVERY12 ALIST0).....2625 / 2.65
    (APPLY3 ALIST0).........2782 / 2.5
    (APPLY2 ALIST0).........2875 / 2.42
    (F6 ALIST0).............3985 / 1.74
    (F4 ALIST0).............4078 / 1.71
    (F_VDH ALIST0)..........6719 / 1.03
    (F7 ALIST0).............6953 / 1 <slowest>

Elapsed milliseconds / relative speed for 262144 iteration(s):
    (F4 ALISTS).............1657 / 4.04 <fastest>
    (F6 ALISTS).............1750 / 3.82
    (VL-REMOVE1 ALISTS).....1906 / 3.51
    (VL-EVERY12 ALISTS).....2000 / 3.34
    (APPLY1 ALISTS).........2047 / 3.27
    (APPLY2 ALISTS).........2953 / 2.26
    (APPLY3 ALISTS).........3125 / 2.14
    (F_VDH ALISTS)..........4672 / 1.43
    (F7 ALISTS).............6687 / 1 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):
    (VL-REMOVE1 ALISTE).....1110 / 3.27 <fastest>
    (APPLY1 ALISTE).........1125 / 3.22
    (VL-EVERY12 ALISTE).....1313 / 2.76
    (APPLY2 ALISTE).........1750 / 2.07
    (APPLY3 ALISTE).........1765 / 2.05
    (F6 ALISTE).............1797 / 2.02
    (F4 ALISTE).............1969 / 1.84
    (F7 ALISTE).............3484 / 1.04
    (F_VDH ALISTE)..........3625 / 1 <slowest>

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1025
  • Marco
Re: null string in list
« Reply #42 on: August 05, 2015, 08:47:59 AM »
Totals:
Code: [Select]
    (VL-REMOVE1 ALIST0).....1828 / 3.8 <fastest>
    (VL-REMOVE1 ALISTS).....1906 / 3.51
    (VL-REMOVE1 ALISTE).....1110 / 3.27 <fastest>
                     Total: 4844
    (APPLY1 ALIST0).........2312 / 3.01
    (APPLY1 ALISTS).........2047 / 3.27
    (APPLY1 ALISTE).........1125 / 3.22
                     Total: 5484
    (VL-EVERY12 ALIST0).....2625 / 2.65
    (VL-EVERY12 ALISTE).....1313 / 2.76
    (VL-EVERY12 ALISTS).....2000 / 3.34
                     Total: 5938
    (F6 ALIST0).............3985 / 1.74
    (F6 ALISTS).............1750 / 3.82
    (F6 ALISTE).............1797 / 2.02
                     Total: 7532
    (APPLY2 ALIST0).........2875 / 2.42
    (APPLY2 ALISTS).........2953 / 2.26
    (APPLY2 ALISTE).........1750 / 2.07
                     Total: 7578
    (APPLY3 ALIST0).........2782 / 2.5
    (APPLY3 ALISTS).........3125 / 2.14
    (APPLY3 ALISTE).........1765 / 2.05
                     Total: 7672
    (F4 ALIST0).............4078 / 1.71
    (F4 ALISTS).............1657 / 4.04 <fastest>
    (F4 ALISTE).............1969 / 1.84
                     Total: 7704
    (F_VDH ALIST0)..........6719 / 1.03
    (F_VDH ALISTE)..........3625 / 1 <slowest>
    (F_VDH ALISTS)..........4672 / 1.43
                     Total:15016
    (F7 ALIST0).............6953 / 1 <slowest>
    (F7 ALISTS).............6687 / 1 <slowest>
    (F7 ALISTE).............3484 / 1.04
                     Total:17124

bruno_vdh

  • Guest
Re: null string in list
« Reply #43 on: August 05, 2015, 09:49:52 AM »
@ Marc'Antonio Alessi, thanks these comparisons are interesting, but I find it hard to do ..

alist0 = alistE 1  Performance comparison
And if alistS very long and it is more difficult for the vl-remove function

Regards,
PS: A list is infinite, how can we say that the list is long?

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1025
  • Marco
Re: null string in list
« Reply #44 on: August 05, 2015, 01:17:28 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun f4 ( l )
  2.     (not (while (= "" (car l)) (setq l (cdr l))))
  3. )
Comando: (f4 '("1" "" "" "" ""))
T
 :wink: