(setq newlist (replace index oldlist newitem))
(defun c:test ()
(setq lst '(0 1 2 3 4 5 6))
(setq new (swapnth lst 3 "A"))
)
;; CAB 11/15/2006
;; replace nth item in list
(defun swapnth (lst i1 itm / tmp nlst)
(while (and (> i1 0)
(< (length (setq nlst (cons (car lst) nlst))) i1))
(setq lst (cdr lst))
)
(setq nlst (cons itm nlst)
lst (cddr lst))
(while (or (setq tmp (car lst)) lst)
(setq nlst (cons tmp nlst)
lst (cdr lst))
)
(reverse nlst)
)
(defun replase (lst i itm)
;;(replase '(0 1 2 3 4 5 6) 3 "A")
;; =>
;; '(0 1 2 "A" 4 5 6)
(mapcar
(function
(lambda (x)
(if (zerop i)
(progn (setq i (1- i)) itm)
(progn (setq i (1- i)) x)
) ;_ if
) ;_ lambda
) ;_ function
lst
) ;_ mapcar
) ;_ defun
Now that is a pretty solution. :):)
(defun replase_1 (lst i itm)
;;(replase_1 '(0 1 2 3 4 5 6) 3 "A")
;; =>
;; '(0 1 2 "A" 4 5 6)
(if lst
(if (> i 0)
(cons (car lst) (replase_1 (cdr lst) (1- i) itm))
(cons itm (cdr lst))
) ;_ if
) ;_ if
)
(defun nth-replace ( pos new-item lst )
;; Nth-Replacer
;;
;; This procedure will itterate thru an
;; entire list to replace the nth item
;; of that list.
;;
;; (nth-replace 2 3 '(1 2 4 4))
;; (1 2 3 4)
;;
;; By: John (Se7en) K
;; 12.27.06
;;
(if (null lst)
nil
(cons
(if (eq pos 0) new-item (car lst))
(nth-replace (1- pos) new-item (cdr lst)))) )
(defun replace (lst i itm)
(setq i (1+ i))
(mapcar
'(lambda (x)
(if (zerop (setq i (1- i))) itm x)
)
lst
)
)
Alas, I feel like I am in the shadows of a Giant.
(defun replase_2 (lst i itm / c)
;;(replase_2 '(0 1 2 3 4 5 6) 3 "A")
;; =>
;; '(0 1 2 "A" 4 5 6)
(setq c -1)
(mapcar
(function cdr)
((lambda (l)
(subst
(cons 0 itm)
(assoc i l)
l
) ;_ subst
) ;_ lambda
(mapcar (function (lambda (x) (cons (setq c (1+ c)) x))) lst)
)
) ;_ mapcar
) ;_ defun
;-------------------------------------------------------------------------------
; Move_nth - Moves the nth Num1# item value to the nth Num2# location in a list.
; Arguments: 3
; Num1# = Nth number in list to move item value
; Num2# = Nth number in list to move item value of nth Num1# into
; OldList@ = List to move item values
; Returns: A list with nth item value moved.
;-------------------------------------------------------------------------------
(defun Move_nth (Num1# Num2# OldList@ / Cnt# Item NewList@ Num1Value Valid)
(if (and (= (type Num1#) 'int)(= (type Num2#) 'int)(= (type OldList@) 'list))
(setq Valid t)
);if
(if (and Valid (< Num1# (length OldList@))(< Num2# (length OldList@))
(/= Num1# Num2#)
);and
(progn
(setq Cnt# 0)
(setq Num1Value (nth Num1# OldList@))
(foreach Item OldList@
(if (/= Cnt# Num1#)
(if (< Num1# Num2#)
(if (/= Cnt# Num2#)
(if NewList@
(setq NewList@ (append NewList@ (list Item)))
(setq NewList@ (list Item))
);if
(progn
(if NewList@
(setq NewList@ (append NewList@ (list Item)))
(setq NewList@ (list Item))
);if
(if NewList@
(setq NewList@ (append NewList@ (list Num1Value)))
(setq NewList@ (list Num1Value))
);if
);progn
);if
(if (/= Cnt# Num2#)
(if NewList@
(setq NewList@ (append NewList@ (list Item)))
(setq NewList@ (list Item))
);if
(progn
(if NewList@
(setq NewList@ (append NewList@ (list Num1Value)))
(setq NewList@ (list Num1Value))
);if
(if NewList@
(setq NewList@ (append NewList@ (list Item)))
(setq NewList@ (list Item))
);if
);progn
);if
);if
);if
(setq Cnt# (1+ Cnt#))
);foreach
);progn
(setq NewList@ OldList@)
);if
NewList@
);defun Move_nth
;; CAB 12/27/2006
;; (RemoveNth 3 '(0 1 2 3 4 5))
;; (0 1 2 4 5)
(defun removeNth (i lst)
(setq i (1+ i))
(vl-remove-if '(lambda(x) (zerop (setq i (1- i)))) lst)
)
;;(RemoveNth 3 '(0 1 2 3 4 5))
(defun removeNth (i lst)
(if (< i (length lst))
(vl-remove (nth i lst) lst)
)
)
(RemoveNth 3 '(3 1 2 3 4 5))
try thisCode: [Select](RemoveNth 3 '(3 1 2 3 4 5))
;;(replace "a" 3 '(0 1 2 3 4 5 6))
(defun replace (a n lst / e i nl)
(setq nl '()
i 0
)
(foreach e lst
(if (= i n)
(setq nl (cons a nl))
(setq nl (cons e nl))
)
(setq i (1+ i))
)
(reverse nl)
)
;;(RemoveNth 3 '(3 0 1 2 3 4 5))
(defun removeNth (n lst / e i nl)
(setq nl '()
i 0
)
(foreach e lst
(if (/= i n)
(setq nl (cons e nl))
)
(setq i (1+ i))
)
(reverse nl)
)
;; CAB 12/27/06
;; move i1 to i2 in list
;; Does not catch i1 or i2 <0 or > (length lst)
(defun MoveNth (i1 i2 lst / idx)
(setq idx -1)
(mapcar '(lambda (x)
(setq idx (1+ idx))
(cond
((= idx i2) (nth i1 lst))
((and (> i1 i2) (or (< idx i2)(> idx i1))) x)
((and (> i2 i1) (>= idx i2)) x)
((and (> i2 i1) (>= idx i1)) (nth (1+ idx) lst))
((> idx i2) (nth (1- idx) lst))
(x)
)
)
lst
)
)
PS Does not catch index out of range, i1 or i2 < 0 or > (length lst)
;-------------------------------------------------------------------------------
; Remove_nths - Removes the RemoveList@ of nths from a list.
; Arguments: 2
; RemoveList@ = List of nths to remove
; OldList@ = List to remove the list of nths from
; Returns: A list with the list of nths removed.
;-------------------------------------------------------------------------------
(defun Remove_nths (RemoveList@ OldList@ / Cnt# Item NewList@)
(setq Cnt# 0)
(foreach Item OldList@
(if (not (member Cnt# RemoveList@))
(setq NewList@ (append NewList@ (list Item)))
);if
(setq Cnt# (1+ Cnt#))
);foreach
NewList@
);defun Remove_nths
(defun Remove_nths (lst1 lst2)
(if (and lst1 lst2)
(if (zerop (car lst1))
(Remove_nths (mapcar '1- (cdr lst1)) (cdr lst2))
(cons (car lst2) (Remove_nths (mapcar '1- lst1) (cdr lst2)))
) ;_ if
lst2
) ;_ if
) ;_ defun
(setq lst1 (vl-sort '(4 20 0) '<) lst2 '("a" "b" "c" "d" "e" "f"))
(Remove_nths lst1 lst2)
=>>
'("b" "c" "d" "f")
(defun MoveNth (i1 i2 lst / MoveNth_1)
(defun MoveNth_1 (i1 i2 lst x i)
(cond
((or (and (> i i1) (> i i2)) (= i1 i2)) lst)
((= i i1) (MoveNth_1 i1 (1+ i2) (cdr lst) x (1+ i)))
((= i i2) (cons x (MoveNth_1 (1+ i1) i2 lst x (1+ i))))
((cons (car lst) (MoveNth_1 i1 i2 (cdr lst) x (1+ i))))
) ;_ cond
) ;_ defun
(MoveNth_1 i1 i2 lst (nth i1 lst) 0)
) ;_ defun
(MoveNth 3 1 '("a" "b" "c" "d" "e" "f"));=> '("a" "d" "b" "c" "e" "f")
(MoveNth 1 3 '("a" "b" "c" "d" "e" "f"));=> '("a" "c" "d" "b" "e" "f")
(MoveNth 1 10 '("a" "b" "c" "d" "e" "f"));=> '("a" "c" "d" "e" "f" nil nil nil nil nil "b")
(MoveNth 10 1 '("a" "b" "c" "d" "e" "f"));=> '("a" nil "b" "c" "d" "e" "f" nil nil nil nil)
nice lesson Evgeniy !!
(defun MoveNth (i1 i2 lst / MoveNth_1)
(defun MoveNth_1 (i1 i2 lst x)
(cond
((or (and (minusp i1) (minusp i2)) (= i1 i2)) lst)
((zerop i1) (MoveNth_1 (1- i1) i2 (cdr lst) x))
((zerop i2) (cons x (MoveNth_1 i1 (1- i2) lst x)))
((cons (car lst) (MoveNth_1 (1- i1) (1- i2) (cdr lst) x)))
) ;_ cond
) ;_ defun
(MoveNth_1 i1 i2 lst (nth i1 lst))
) ;_ defun
The versions of moventh presented so far return the same results if the arguments for the nths are possible in the list. My version returns the original list if the arguments for the nths are not possible. I’m not comfortable with adding nils in a list where you might be expecting some other type of data. That means you would have to always have to double check if the value you need is a real value.
(defun MoveNth (i1 i2 lst / MoveNth_1)
(defun MoveNth_1 (i1 i2 lst x i)
(cond
((and (> i i1) (> i i2)) lst)
((= i i1) (MoveNth_1 i1 (1+ i2) (cdr lst) x (1+ i)))
((= i i2) (cons x (MoveNth_1 (1+ i1) i2 lst x (1+ i))))
((cons (car lst) (MoveNth_1 i1 i2 (cdr lst) x (1+ i))))
) ;_ cond
) ;_ defun
(if (and (/= i1 i2) (<= 0 i1 (length lst)) (<= 0 i2 (length lst)))
(MoveNth_1 i1 i2 lst (nth i1 lst) 0)
lst
) ;_ if
) ;_ defun
;; CAB 12/28/06
;; move i1 to i2 in list
(defun MoveNth (i1 i2 lst / idx)
(setq idx -1)
(if (and (< -1 i1 (length lst)) (< -1 i2 (length lst)))
(mapcar '(lambda (x)
(setq idx (1+ idx))
(cond
((= idx i2) (nth i1 lst))
((and (> i1 i2) (or (< idx i2)(> idx i1))) x)
((and (> i2 i1) (>= idx i2)) x)
((and (> i2 i1) (>= idx i1)) (nth (1+ idx) lst))
((> idx i2) (nth (1- idx) lst))
(x)
)
)
lst
)
lst ; remove this line if you want nil returned if out of range
)
)
(defun c:test ()
(print (MoveNth 3 1 '("a" "b" "c" "d" "e" "f")))
(print (MoveNth 1 3 '("a" "b" "c" "d" "e" "f")))
(print (MoveNth 1 10 '("a" "b" "c" "d" "e" "f")))
(print (MoveNth 10 1 '("a" "b" "c" "d" "e" "f")))
(princ)
)
("a" "d" "b" "c" "e" "f")
("a" "c" "d" "b" "e" "f")
("a" "b" "c" "d" "e" "f")
("a" "b" "c" "d" "e" "f")
As Evgeniy said, "No Problem" :)
You guys are the greatest. This is a fun thread.You should come and visit more often .. there are lots of other threads very similar to this one.
The checks in the mail. (ha!)
You guys are the greatest. This is a fun thread.
The checks in the mail. (ha!)
Command: TestNths
swapnth: 21543086
replase: 21544061
nth-replace: 21544324
replace: 21544728
replase_2: 21544986
Finished: 21545784
Lowest number for results is the fastest:
swapnth = 975
replase = 263
nth-replace = 404
replace = 258
replase_2 = 798
Command: TestNths
swapnth: 21550007
replase: 21550971
nth-replace: 21551233
replace: 21551635
replase_2: 21551893
Finished: 21552689
Lowest number for results is the fastest:
swapnth = 964
replase = 262
nth-replace = 402
replace = 258
replase_2 = 796
Command: TestNths
swapnth: 21593135
replase: 21594107
nth-replace: 21594370
replace: 21594772
replase_2: 21595030
Finished: 21595830
Lowest number for results is the fastest:
swapnth = 972
replase = 263
nth-replace = 402
replace = 258
replase_2 = 800
Command: TestNths
swapnth: 22000948
replase: 22001915
nth-replace: 22002177
replace: 22002579
replase_2: 22002837
Finished: 22003632
Lowest number for results is the fastest:
swapnth = 967
replase = 262
nth-replace = 402
replace = 258
replase_2 = 795
I ran this test several times and it returned similar results.(defun c:TestNths (/ Cnt# t1 t2 t3 t4 t5 t6)
(if (not *List@);Create a 1000 item global *List@
(repeat 10
(setq Cnt# 48);Start at (ascii "0") (chr 48)
(repeat 100
(setq *List@ (append *List@ (list (chr Cnt#))))
(setq Cnt# (1+ Cnt#))
);repeat
);repeat
);if
(princ "\nswapnth: ")
(princ (setq t1 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (swapnth *List@ 998 "NEW"))
(princ "\nreplase: ")
(princ (setq t2 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (replase *List@ 998 "NEW"))
(princ "\nnth-replace: ")
(princ (setq t3 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (nth-replace 998 "NEW" *List@))
(princ "\nreplace: ")
(princ (setq t4 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (replace *List@ 998 "NEW"))
(princ "\nreplase_2: ")
(princ (setq t5 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (replase_2 *List@ 998 "NEW"))
(princ "\nFinished: ")
(princ (setq t6 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(princ "\nLowest number for results is the fastest:")
(princ "\nswapnth = ") (princ (- t2 t1))
(princ "\nreplase = ") (princ (- t3 t2))
(princ "\nnth-replace = ")(princ (- t4 t3))
(princ "\nreplace = ") (princ (- t5 t4))
(princ "\nreplase_2 = ") (princ (- t6 t5))
(princ)
);defun c:TestNths
;; CAB 11/15/2006
;; replace nth item in list
(defun swapnth (lst i1 itm / tmp nlst)
(while (and (> i1 0)
(< (length (setq nlst (cons (car lst) nlst))) i1))
(setq lst (cdr lst))
)
(setq nlst (cons itm nlst)
lst (cddr lst))
(while (or (setq tmp (car lst)) lst)
(setq nlst (cons tmp nlst)
lst (cdr lst))
)
(reverse nlst)
)
(defun replase (lst i itm)
;;(replase '(0 1 2 3 4 5 6) 3 "A")
;; =>
;; '(0 1 2 "A" 4 5 6)
(mapcar
(function
(lambda (x)
(if (zerop i)
(progn (setq i (1- i)) itm)
(progn (setq i (1- i)) x)
) ;_ if
) ;_ lambda
) ;_ function
lst
) ;_ mapcar
) ;_ defun
(defun replase_1 (lst i itm)
;;(replase_1 '(0 1 2 3 4 5 6) 3 "A")
;; =>
;; '(0 1 2 "A" 4 5 6)
(if lst
(if (> i 0)
(cons (car lst) (replase_1 (cdr lst) (1- i) itm))
(cons itm (cdr lst))
) ;_ if
) ;_ if
)
(defun nth-replace ( pos new-item lst )
;; Nth-Replacer
;;
;; This procedure will itterate thru an
;; entire list to replace the nth item
;; of that list.
;;
;; (nth-replace 2 3 '(1 2 4 4))
;; (1 2 3 4)
;;
;; By: John (Se7en) K
;; 12.27.06
;;
(if (null lst)
nil
(cons
(if (eq pos 0) new-item (car lst))
(nth-replace (1- pos) new-item (cdr lst)))) )
(defun replace (lst i itm)
(setq i (1+ i))
(mapcar
'(lambda (x)
(if (zerop (setq i (1- i))) itm x)
)
lst
)
)
(defun replase_2 (lst i itm / c)
;;(replase_2 '(0 1 2 3 4 5 6) 3 "A")
;; =>
;; '(0 1 2 "A" 4 5 6)
(setq c -1)
(mapcar
(function cdr)
((lambda (l)
(subst
(cons 0 itm)
(assoc i l)
l
) ;_ subst
) ;_ lambda
(mapcar (function (lambda (x) (cons (setq c (1+ c)) x))) lst)
)
) ;_ mapcar
) ;_ defun
(defun c:TestNths (/ Cnt# t1 t2 t3 t4 t5 t6)
(if (not *List@);Create a 1000 item global *List@
(repeat 10
(setq Cnt# 48);Start at (ascii "0") (chr 48)
(repeat 100
(setq *List@ (append *List@ (list (chr Cnt#))))
(setq Cnt# (1+ Cnt#))
);repeat
);repeat
);if
(princ "\nswapnth: ")
(princ (setq t1 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (swapnth *List@ 998 "NEW"))
(princ "\nreplase: ")
(princ (setq t2 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (replase *List@ 998 "NEW"))
(princ "\nnth-replace: ")
(princ (setq t3 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (nth-replace 998 "NEW" *List@))
(princ "\nreplace: ")
(princ (setq t4 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (replace *List@ 998 "NEW"))
(princ "\nreplase_1: ")
(princ (setq t5 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (replase_1 *List@ 998 "NEW"))
(princ "\nreplase_2: ")
(princ (setq t6 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(repeat 1000 (replase_2 *List@ 998 "NEW"))
(princ "\nFinished: ")
(princ (setq t7 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
(princ "\nLowest number for results is the fastest:")
(princ "\nswapnth = ") (princ (- t2 t1))
(princ "\nreplase = ") (princ (- t3 t2))
(princ "\nnth-replace = ")(princ (- t4 t3))
(princ "\nreplace = ") (princ (- t5 t4))
(princ "\nreplase_1 = ") (princ (- t6 t5))
(princ "\nreplase_2 = ") (princ (- t7 t6))
(princ)
);defun c:TestNths
Command:
Command: (LOAD "D:/replase.VLX") nil
Command: 'VLIDE
Command:
Command: TestNths
swapnth: 11341072
replase: 11341546
nth-replace: 11341621
replace: 11341769
replase_1: 11341891
replase_2: 11342038
Finished: 11342197
Lowest number for results is the fastest:
swapnth = 474
replase = 75
nth-replace = 148
replace = 122
replase_1 = 147
replase_2 = 159
Command:
Command: TestNths
swapnth: 11344692
replase: 11345168
nth-replace: 11345246
replace: 11345398
replase_1: 11345538
replase_2: 11345685
Finished: 11345851
Lowest number for results is the fastest:
swapnth = 476
replase = 78
nth-replace = 152
replace = 140
replase_1 = 147
replase_2 = 166
Command:
Command: TestNths
swapnth: 11350462
replase: 11350933
nth-replace: 11351009
replace: 11351155
replase_1: 11351276
replase_2: 11351423
Finished: 11351581
Lowest number for results is the fastest:
swapnth = 471
replase = 76
nth-replace = 146
replace = 121
replase_1 = 147
replase_2 = 158
Command:
Command: TestNths
swapnth: 11352441
replase: 11352913
nth-replace: 11352990
replace: 11353141
replase_1: 11353269
replase_2: 11353420
Finished: 11353582
Lowest number for results is the fastest:
swapnth = 472
replase = 77
nth-replace = 151
replace = 128
replase_1 = 151
replase_2 = 162
Command:
Command: TestNths
swapnth: 11354036
replase: 11354512
nth-replace: 11354588
replace: 11354739
replase_1: 11354871
replase_2: 11355021
Finished: 11355184
Lowest number for results is the fastest:
swapnth = 476
replase = 76
nth-replace = 151
replace = 132
replase_1 = 150
replase_2 = 163
Command:
Command:
Command: (LOAD "D:/replase.LSP") C:TESTNTHS
Command: TestNths
swapnth: 11533020
replase: 11533675
nth-replace: 11533829
replace: 11534141
replase_1: 11534278
replase_2: 11534565
Finished: 11534774
Lowest number for results is the fastest:
swapnth = 655
replase = 154
nth-replace = 312
replace = 137
replase_1 = 287
replase_2 = 209
Command:
Command: TestNths
swapnth: 11535754
replase: 11540417
nth-replace: 11540575
replace: 11540888
replase_1: 11541032
replase_2: 11541324
Finished: 11541538
Lowest number for results is the fastest:
swapnth = 4663
replase = 158
nth-replace = 313
replace = 144
replase_1 = 292
replase_2 = 214
Command:
Command: TestNths
swapnth: 11541771
replase: 11542436
nth-replace: 11542590
replace: 11542902
replase_1: 11543051
replase_2: 11543344
Finished: 11543559
Lowest number for results is the fastest:
swapnth = 665
replase = 154
nth-replace = 312
replace = 149
replase_1 = 293
replase_2 = 215
Command:
Command: TestNths
swapnth: 11543785
replase: 11544473
nth-replace: 11544641
replace: 11544969
replase_1: 11545104
replase_2: 11545421
Finished: 11545646
Lowest number for results is the fastest:
swapnth = 688
replase = 168
nth-replace = 328
replace = 135
replase_1 = 317
replase_2 = 225
Command:
Command: TestNths
swapnth: 11545831
replase: 11550502
nth-replace: 11550670
replace: 11550990
replase_1: 11551142
replase_2: 11551450
Finished: 11551669
Lowest number for results is the fastest:
swapnth = 4671
replase = 168
nth-replace = 320
replace = 152
replase_1 = 308
replase_2 = 219
Command:
;;=================================================================
;;
;; Example:
;;
;; (BenchMark
;; '(
;; (1+ 1)
;; (+ 1 1)
;; (+ 1 1.0)
;; (+ 1.0 1.0)
;; )
;; )
;;
;;=================================================================
;;
;; Output:
;;
;; Elapsed milliseconds / relative speed for 32768 iteration(s):
;;
;; (1+ 1)..........1969 / 1.09 <fastest>
;; (+ 1 1).........2078 / 1.03
;; (+ 1 1.0).......2125 / 1.01
;; (+ 1.0 1.0).....2140 / 1.00 <slowest>
;;
;;=================================================================
;; .... >>>> etc
Benchmarking [M.P. @ theSwamp 2005] ..................
Elapsed milliseconds for 32768 iteration(s)/ relative Timing :
(+ 1 1.0).......1735 / 1.0188 <slowest>
(+ 1.0 1.0).....1735 / 1.0188
(+ 1 1).........1719 / 1.0094
(1+ 1)..........1703 / 1 <fastest>
Command: test
Elapsed milliseconds / relative speed for 16384 iteration(s):
(REPLASE_1 (QUOTE (0 1 2 3 4 5 6)) 3...).....1973 / 1.15 <fastest>
(SWAPNTH (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2113 / 1.08
(NTH-REPLACE 3 "a" (QUOTE (0 1 2 3 4...).....2193 / 1.04
(REPLACE_CAB (QUOTE (0 1 2 3 4 5 6))...).....2193 / 1.04
(REPLASE (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2214 / 1.03
(REPLACE_DA "a" 3 (QUOTE (0 1 2 3 4 ...).....2273 / 1.00 <slowest>
Command:
Command: test
Elapsed milliseconds / relative speed for 16384 iteration(s):
(REPLASE_1 (QUOTE (0 1 2 3 4 5 6)) 3...).....1983 / 1.15 <fastest>
(SWAPNTH (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2123 / 1.08
(REPLACE_CAB (QUOTE (0 1 2 3 4 5 6))...).....2203 / 1.04
(NTH-REPLACE 3 "a" (QUOTE (0 1 2 3 4...).....2204 / 1.04
(REPLASE (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2223 / 1.03
(REPLACE_DA "a" 3 (QUOTE (0 1 2 3 4 ...).....2283 / 1.00 <slowest>
Command:
Command: test
Elapsed milliseconds / relative speed for 16384 iteration(s):
(REPLASE_1 (QUOTE (0 1 2 3 4 5 6)) 3...).....2003 / 1.14 <fastest>
(SWAPNTH (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2123 / 1.08
(NTH-REPLACE 3 "a" (QUOTE (0 1 2 3 4...).....2204 / 1.04
(REPLACE_CAB (QUOTE (0 1 2 3 4 5 6))...).....2223 / 1.03
(REPLASE (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2233 / 1.02
(REPLACE_DA "a" 3 (QUOTE (0 1 2 3 4 ...).....2283 / 1.00 <slowest>
(defun c:test ()
(BenchMark
'((replace_Da "a" 3 '(0 1 2 3 4 5 6))
(nth-replace 3 "a" '(0 1 2 3 4 5 6))
(swapnth '(0 1 2 3 4 5 6) 3 "A")
(replase '(0 1 2 3 4 5 6) 3 "A")
(replace_CAB '(0 1 2 3 4 5 6) 3 "A")
(replase_1 '(0 1 2 3 4 5 6) 3 "A")
)
)
)
;; Danielm103
;;(replace "a" 3 '(0 1 2 3 4 5 6))
(defun replace_Da (a n lst / e i nl)
(setq nl '()
i 0
)
(foreach e lst
(if (= i n)
(setq nl (cons a nl))
(setq nl (cons e nl))
)
(setq i (1+ i))
)
(reverse nl)
)
;; CAB 11/15/2006
;; replace nth item in list
(defun swapnth (lst i1 itm / tmp nlst)
(while (and (> i1 0)
(< (length (setq nlst (cons (car lst) nlst))) i1))
(setq lst (cdr lst))
)
(setq nlst (cons itm nlst)
lst (cddr lst))
(while (or (setq tmp (car lst)) lst)
(setq nlst (cons tmp nlst)
lst (cdr lst))
)
(reverse nlst)
)
;; Evigeny
(defun replase (lst i itm)
;;(replase '(0 1 2 3 4 5 6) 3 "A")
;; =>
;; '(0 1 2 "A" 4 5 6)
(mapcar
(function
(lambda (x)
(if (zerop i)
(progn (setq i (1- i)) itm)
(progn (setq i (1- i)) x)
)
)
)
lst
)
)
;; variation by CAB
(defun replace_CAB (lst i itm)
(setq i (1+ i))
(mapcar '(lambda (x) (if (zerop (setq i (1- i))) itm x)) lst)
)
;; Evigeny
(defun replase_1 (lst i itm)
;;(replase_1 '(0 1 2 3 4 5 6) 3 "A")
;; =>
;; '(0 1 2 "A" 4 5 6)
(if lst
(if (> i 0)
(cons (car lst) (replase_1 (cdr lst) (1- i) itm))
(cons itm (cdr lst))
) ;_ if
) ;_ if
)
(defun nth-replace ( pos new-item lst )
;; Nth-Replacer
;;
;; This procedure will itterate thru an
;; entire list to replace the nth item
;; of that list.
;;
;; (nth-replace 2 3 '(1 2 4 4))
;; (1 2 3 4)
;;
;; By: John (Se7en) K
;; 12.27.06
;;
(if (null lst)
nil
(cons
(if (eq pos 0) new-item (car lst))
(nth-replace (1- pos) new-item (cdr lst)))) )
(defun c:test ()
(BenchMark
'((replace_Da "NEW" 998 *List@)
(nth-replace 998 "NEW" *List@)
(swapnth *List@ 998 "NEW")
(replase *List@ 998 "NEW")
(replace_CAB *List@ 998 "NEW")
(replase_1 *List@ 998 "NEW")
)
)
)
Command: test
Elapsed milliseconds / relative speed for 512 iteration(s):
(REPLACE_CAB *LIST@ 998 "NEW").....1011 / 3.90 <fastest>
(REPLASE *LIST@ 998 "NEW").........1072 / 3.68
(REPLASE_1 *LIST@ 998 "NEW").......1833 / 2.15
(REPLACE_DA "NEW" 998 *LIST@)......1902 / 2.07
(NTH-REPLACE 998 "NEW" *LIST@).....1952 / 2.02
(SWAPNTH *LIST@ 998 "NEW").........3946 / 1.00 <slowest>
Command:
Command: test
Elapsed milliseconds / relative speed for 512 iteration(s):
(REPLACE_CAB *LIST@ 998 "NEW").....1011 / 3.90 <fastest>
(REPLASE *LIST@ 998 "NEW").........1072 / 3.68
(REPLASE_1 *LIST@ 998 "NEW").......1833 / 2.15
(REPLACE_DA "NEW" 998 *LIST@)......1923 / 2.05
(NTH-REPLACE 998 "NEW" *LIST@).....2083 / 1.89
(SWAPNTH *LIST@ 998 "NEW").........3946 / 1.00 <slowest>
(defun c:test ()
(BenchMark
'((replace_Da "NEW" 5 *List@)
(nth-replace 998 "NEW" *List@)
(swapnth *List@ 5 "NEW")
(replase *List@ 5 "NEW")
(replace_CAB *List@ 5 "NEW")
(replase_1 *List@ 5 "NEW")
)
)
)
WOW - what do you suppose is going on here.. :)Command: test
Elapsed milliseconds / relative speed for 8192 iteration(s):
(REPLASE_1 *LIST@ 5 "NEW")........1011 / 30.91 <fastest>
(REPLACE_CAB *LIST@ 5 "NEW").....16043 / 1.95
(REPLASE *LIST@ 5 "NEW").........17195 / 1.82
(SWAPNTH *LIST@ 5 "NEW").........22052 / 1.42
(REPLACE_DA "NEW" 5 *LIST@)......30293 / 1.03
(NTH-REPLACE 5 "NEW" *LIST@).....31245 / 1.00 <slowest>
Command:
Command: test
Elapsed milliseconds / relative speed for 8192 iteration(s):
(REPLASE_1 *LIST@ 5 "NEW")........1021 / 30.65 <fastest>
(REPLACE_CAB *LIST@ 5 "NEW").....16123 / 1.94
(REPLASE *LIST@ 5 "NEW").........17335 / 1.81
(SWAPNTH *LIST@ 5 "NEW").........22082 / 1.42
(REPLACE_DA "NEW" 5 *LIST@)......30504 / 1.03
(NTH-REPLACE 5 "NEW" *LIST@).....31295 / 1.00 <slowest>
In your recursive routine, it stops when the replacement is made so in that test
it stopped after 6 iterations.
No, sorry, I was too lazy. :oops:
(defun c:test (/ *LIST@ CNT# I)
(if (not *List@)
(repeat 10
(setq Cnt# 48)
(repeat 100
(setq *List@ (append *List@ (list (chr Cnt#))))
(setq Cnt# (1+ Cnt#))
) ;repeat
) ;repeat
) ;_ if
(setq i 99)
(repeat 10
(princ (strcat "\n\nI = " (itoa i) "\n"))
(BenchMark
'((replace_Da "A" i *List@)
(nth-replace i "A" *List@)
(swapnth *List@ i "A")
(replase *List@ i "A")
(replace_CAB *List@ i "A")
(replase_1 *List@ i "A")
(replase_2 *List@ i "A")
)
) ;_ BenchMark
(setq i (+ i 100))
) ;_ repeat
) ;_ defun
;; test.lsp
(defun replace_Da (a n lst / e i nl)
;; Danielm103
(setq nl '()
i 0
) ;_ setq
(foreach e lst
(if (= i n)
(setq nl (cons a nl))
(setq nl (cons e nl))
) ;_ if
(setq i (1+ i))
) ;_ foreach
(reverse nl)
) ;_ defun
(defun swapnth (lst i1 itm / tmp nlst)
;; CAB 11/15/2006
;; replace nth item in list
(while (and (> i1 0)
(< (length (setq nlst (cons (car lst) nlst))) i1)
) ;_ and
(setq lst (cdr lst))
) ;_ while
(setq nlst (cons itm nlst)
lst (cddr lst)
) ;_ setq
(while (or (setq tmp (car lst)) lst)
(setq nlst (cons tmp nlst)
lst (cdr lst)
) ;_ setq
) ;_ while
(reverse nlst)
) ;_ defun
(defun replase (lst i itm)
;; ElpanovEvgeniy
(mapcar
(function
(lambda (x)
(if (zerop i)
(progn (setq i (1- i)) itm)
(progn (setq i (1- i)) x)
) ;_ if
) ;_ lambda
) ;_ function
lst
) ;_ mapcar
) ;_ defun
(defun replace_CAB (lst i itm)
;; variation by CAB
(setq i (1+ i))
(mapcar '(lambda (x)
(if (zerop (setq i (1- i)))
itm
x
) ;_ if
) ;_ lambda
lst
) ;_ mapcar
) ;_ defun
(defun replase_1 (lst i itm)
;; ElpanovEvgeniy
(if lst
(if (> i 0)
(cons (car lst) (replase_1 (cdr lst) (1- i) itm))
(cons itm (cdr lst))
) ;_ if
) ;_ if
) ;_ defun
(defun nth-replace (pos new-item lst)
;; By: John (Se7en) K 12.27.06
(if (null lst)
nil
(cons
(if (eq pos 0)
new-item
(car lst)
) ;_ if
(nth-replace (1- pos) new-item (cdr lst))
) ;_ cons
) ;_ if
) ;_ defun
(defun replase_2 (lst i itm / c)
;; ElpanovEvgeniy
(setq c -1)
(mapcar
(function cdr)
((lambda (l)
(subst
(cons 0 itm)
(assoc i l)
l
) ;_ subst
) ;_ lambda
(mapcar (function (lambda (x) (cons (setq c (1+ c)) x))) lst)
)
) ;_ mapcar
)
Command:
Command: (LOAD "E:/test.LSP") C:TEST
Command: test
I = 99
Elapsed milliseconds / relative speed for 4096 iteration(s):
(REPLASE_1 *LIST@ I "A")........1437 / 9.99 <fastest>
(REPLACE_CAB *LIST@ I "A")......6625 / 2.17
(REPLASE *LIST@ I "A")..........7187 / 2.00
(REPLASE_2 *LIST@ I "A").......10265 / 1.40
(SWAPNTH *LIST@ I "A").........10313 / 1.39
(NTH-REPLACE I "A" *LIST@).....13812 / 1.04
(REPLACE_DA "A" I *LIST@)......14359 / 1.00 <slowest>
I = 199
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLASE_1 *LIST@ I "A").......1328 / 5.33 <fastest>
(REPLACE_CAB *LIST@ I "A").....3234 / 2.19
(REPLASE *LIST@ I "A").........3656 / 1.94
(REPLASE_2 *LIST@ I "A").......5141 / 1.38
(SWAPNTH *LIST@ I "A").........5766 / 1.23
(NTH-REPLACE I "A" *LIST@).....6906 / 1.02
(REPLACE_DA "A" I *LIST@)......7078 / 1.00 <slowest>
I = 299
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLASE_1 *LIST@ I "A").......1016 / 3.41 <fastest>
(REPLACE_CAB *LIST@ I "A").....1672 / 2.07
(REPLASE *LIST@ I "A").........1828 / 1.90
(REPLASE_2 *LIST@ I "A").......2531 / 1.37
(SWAPNTH *LIST@ I "A").........3156 / 1.10
(NTH-REPLACE I "A" *LIST@).....3391 / 1.02
(REPLACE_DA "A" I *LIST@)......3469 / 1.00 <slowest>
I = 399
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLASE_1 *LIST@ I "A").......1297 / 2.70 <fastest>
(REPLACE_CAB *LIST@ I "A").....1672 / 2.09
(REPLASE *LIST@ I "A").........1781 / 1.97
(REPLASE_2 *LIST@ I "A").......2594 / 1.35
(NTH-REPLACE I "A" *LIST@).....3484 / 1.00
(REPLACE_DA "A" I *LIST@)......3500 / 1.00
(SWAPNTH *LIST@ I "A").........3500 / 1.00 <slowest>
I = 499
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A").....1578 / 2.53 <fastest>
(REPLASE_1 *LIST@ I "A").......1657 / 2.40
(REPLASE *LIST@ I "A").........1813 / 2.20
(REPLASE_2 *LIST@ I "A").......2610 / 1.53
(NTH-REPLACE I "A" *LIST@).....3422 / 1.16
(REPLACE_DA "A" I *LIST@)......3547 / 1.12
(SWAPNTH *LIST@ I "A").........3985 / 1.00 <slowest>
I = 599
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A").....1671 / 2.58 <fastest>
(REPLASE *LIST@ I "A").........1829 / 2.36
(REPLASE_1 *LIST@ I "A").......1969 / 2.19
(REPLASE_2 *LIST@ I "A").......2532 / 1.70
(REPLACE_DA "A" I *LIST@)......3468 / 1.24
(NTH-REPLACE I "A" *LIST@).....3516 / 1.23
(SWAPNTH *LIST@ I "A").........4313 / 1.00 <slowest>
I = 699
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A").....1641 / 3.06 <fastest>
(REPLASE *LIST@ I "A").........1828 / 2.74
(REPLASE_1 *LIST@ I "A").......2281 / 2.20
(REPLASE_2 *LIST@ I "A").......2563 / 1.96
(NTH-REPLACE I "A" *LIST@).....3390 / 1.48
(REPLACE_DA "A" I *LIST@)......3594 / 1.40
(SWAPNTH *LIST@ I "A").........5016 / 1.00 <slowest>
I = 799
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A").....1593 / 3.58 <fastest>
(REPLASE *LIST@ I "A").........1829 / 3.12
(REPLASE_2 *LIST@ I "A").......2609 / 2.19
(REPLASE_1 *LIST@ I "A").......2625 / 2.17
(NTH-REPLACE I "A" *LIST@).....3375 / 1.69
(REPLACE_DA "A" I *LIST@)......3609 / 1.58
(SWAPNTH *LIST@ I "A").........5703 / 1.00 <slowest>
I = 899
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A").....1562 / 4.08 <fastest>
(REPLASE *LIST@ I "A").........1828 / 3.49
(REPLASE_2 *LIST@ I "A").......2610 / 2.44
(REPLASE_1 *LIST@ I "A").......2938 / 2.17
(REPLACE_DA "A" I *LIST@)......3328 / 1.92
(NTH-REPLACE I "A" *LIST@).....3453 / 1.85
(SWAPNTH *LIST@ I "A").........6375 / 1.00 <slowest>
I = 999
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A").....1421 / 5.00 <fastest>
(REPLASE *LIST@ I "A").........1813 / 3.92
(REPLASE_2 *LIST@ I "A").......2547 / 2.79
(REPLASE_1 *LIST@ I "A").......3313 / 2.15
(REPLACE_DA "A" I *LIST@)......3454 / 2.06
(NTH-REPLACE I "A" *LIST@).....3516 / 2.02
(SWAPNTH *LIST@ I "A").........7110 / 1.00 <slowest>
1099
Command:
Command:
Command: (LOAD "E:/test.VLX") nil
Command: test
I = 99
Elapsed milliseconds / relative speed for 8192 iteration(s):
(REPLASE_1 *LIST@ I "A")........1719 / 8.83 <fastest>
(REPLASE *LIST@ I "A")..........7094 / 2.14
(SWAPNTH *LIST@ I "A")..........8985 / 1.69
(REPLACE_DA "A" I *LIST@).......9375 / 1.62
(REPLACE_CAB *LIST@ I "A").....10156 / 1.49
(NTH-REPLACE I "A" *LIST@).....13047 / 1.16
(REPLASE_2 *LIST@ I "A").......15172 / 1.00 <slowest>
I = 199
Elapsed milliseconds / relative speed for 4096 iteration(s):
(REPLASE_1 *LIST@ I "A").......1500 / 5.11 <fastest>
(REPLASE *LIST@ I "A").........3547 / 2.16
(REPLACE_DA "A" I *LIST@)......4688 / 1.64
(REPLACE_CAB *LIST@ I "A").....5110 / 1.50
(SWAPNTH *LIST@ I "A").........5187 / 1.48
(NTH-REPLACE I "A" *LIST@).....6500 / 1.18
(REPLASE_2 *LIST@ I "A").......7672 / 1.00 <slowest>
I = 299
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLASE_1 *LIST@ I "A").......1046 / 3.65 <fastest>
(REPLASE *LIST@ I "A").........1766 / 2.16
(REPLACE_DA "A" I *LIST@)......2375 / 1.61
(REPLACE_CAB *LIST@ I "A").....2516 / 1.52
(SWAPNTH *LIST@ I "A").........3062 / 1.25
(NTH-REPLACE I "A" *LIST@).....3250 / 1.17
(REPLASE_2 *LIST@ I "A").......3813 / 1.00 <slowest>
I = 399
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLASE_1 *LIST@ I "A").......1360 / 2.80 <fastest>
(REPLASE *LIST@ I "A").........1781 / 2.14
(REPLACE_DA "A" I *LIST@)......2360 / 1.62
(REPLACE_CAB *LIST@ I "A").....2547 / 1.50
(NTH-REPLACE I "A" *LIST@).....3250 / 1.17
(SWAPNTH *LIST@ I "A").........3672 / 1.04
(REPLASE_2 *LIST@ I "A").......3813 / 1.00 <slowest>
I = 499
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLASE_1 *LIST@ I "A").......1688 / 2.60 <fastest>
(REPLASE *LIST@ I "A").........1782 / 2.46
(REPLACE_DA "A" I *LIST@)......2328 / 1.89
(REPLACE_CAB *LIST@ I "A").....2547 / 1.72
(NTH-REPLACE I "A" *LIST@).....3265 / 1.34
(REPLASE_2 *LIST@ I "A").......3828 / 1.15
(SWAPNTH *LIST@ I "A").........4390 / 1.00 <slowest>
I = 599
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLASE *LIST@ I "A").........1765 / 2.97 <fastest>
(REPLASE_1 *LIST@ I "A").......2000 / 2.62
(REPLACE_DA "A" I *LIST@)......2344 / 2.23
(REPLACE_CAB *LIST@ I "A").....2563 / 2.04
(NTH-REPLACE I "A" *LIST@).....3265 / 1.60
(REPLASE_2 *LIST@ I "A").......3828 / 1.37
(SWAPNTH *LIST@ I "A").........5234 / 1.00 <slowest>
I = 699
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLASE *LIST@ I "A").........1766 / 3.53 <fastest>
(REPLASE_1 *LIST@ I "A").......2297 / 2.71
(REPLACE_DA "A" I *LIST@)......2344 / 2.66
(REPLACE_CAB *LIST@ I "A").....2547 / 2.45
(NTH-REPLACE I "A" *LIST@).....3266 / 1.91
(REPLASE_2 *LIST@ I "A").......3828 / 1.63
(SWAPNTH *LIST@ I "A").........6235 / 1.00 <slowest>
I = 799
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLASE *LIST@ I "A").........1781 / 4.13 <fastest>
(REPLACE_DA "A" I *LIST@)......2344 / 3.14
(REPLACE_CAB *LIST@ I "A").....2563 / 2.87
(REPLASE_1 *LIST@ I "A").......2625 / 2.80
(NTH-REPLACE I "A" *LIST@).....3250 / 2.26
(REPLASE_2 *LIST@ I "A").......3859 / 1.91
(SWAPNTH *LIST@ I "A").........7359 / 1.00 <slowest>
I = 899
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLASE *LIST@ I "A").........1766 / 4.88 <fastest>
(REPLACE_DA "A" I *LIST@)......2344 / 3.67
(REPLACE_CAB *LIST@ I "A").....2547 / 3.38
(REPLASE_1 *LIST@ I "A").......2922 / 2.95
(NTH-REPLACE I "A" *LIST@).....3265 / 2.64
(REPLASE_2 *LIST@ I "A").......3844 / 2.24
(SWAPNTH *LIST@ I "A").........8610 / 1.00 <slowest>
I = 999
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLASE *LIST@ I "A").........1781 / 5.61 <fastest>
(REPLACE_DA "A" I *LIST@)......2343 / 4.26
(REPLACE_CAB *LIST@ I "A").....2578 / 3.87
(REPLASE_1 *LIST@ I "A").......3235 / 3.09
(NTH-REPLACE I "A" *LIST@).....3250 / 3.07
(REPLASE_2 *LIST@ I "A").......3860 / 2.59
(SWAPNTH *LIST@ I "A").........9984 / 1.00 <slowest>
1099
Command:
Thanks Evgeniy,
You can now see how the position of the index in the list affects the time required.
Also the compiling surprised me as I thought it would be a uniform change in the speed & not the order.
Funny how some methods fair better when compiled.
(defun replace_CAB (lst i itm)
;; variation by CAB
(setq i (1+ i))
(mapcar '(lambda (x)
(if (zerop (setq i (1- i)))
itm
x
) ;_ if
) ;_ lambda
lst
) ;_ mapcar
) ;_ defun
(defun replace_CAB_optim (lst i itm)
;; variation by CAB
(setq i (1+ i))
(mapcar
(function
(lambda (x)
(if (zerop (setq i (1- i)))
itm
x
) ;_ if
) ;_ lambda
) ;_ function
lst
) ;_ mapcar
) ;_ defun
(defun c:test (/ *LIST@ CNT# I)
(if (not *List@)
(repeat 10
(setq Cnt# 48)
(repeat 100
(setq *List@ (append *List@ (list (chr Cnt#))))
(setq Cnt# (1+ Cnt#))
) ;repeat
) ;repeat
) ;_ if
(setq i 99)
(repeat 10
(princ (strcat "\n\nI = " (itoa i) "\n"))
(BenchMark
'((replace_CAB *List@ i "A")
(replace_CAB_optim *List@ i "A")
)
) ;_ BenchMark
(setq i (+ i 100))
) ;_ repeat
) ;_ defun
Command: (LOAD "E:/test_1.LSP") C:TEST
Command: test
I = 99
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1453 / 1.03 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1500 / 1.00 <slowest>
I = 199
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1407 / 1.07 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1500 / 1.00 <slowest>
I = 299
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1422 / 1.02 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1454 / 1.00 <slowest>
I = 399
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1406 / 1.04 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1469 / 1.00 <slowest>
I = 499
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1422 / 1.03 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1469 / 1.00 <slowest>
I = 599
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1422 / 1.03 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1469 / 1.00 <slowest>
I = 699
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1407 / 1.06 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1485 / 1.00 <slowest>
I = 799
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1406 / 1.06 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1484 / 1.00 <slowest>
I = 899
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1422 / 1.04 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1485 / 1.00 <slowest>
I = 999
Elapsed milliseconds / relative speed for 1024 iteration(s):
(REPLACE_CAB *LIST@ I "A")...........1422 / 1.02 <fastest>
(REPLACE_CAB_OPTIM *LIST@ I "A").....1453 / 1.00 <slowest>
1099
Command:
Command:
Command: (LOAD "E:/test_1.VLX") nil
Command: test
I = 99
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1719 / 1.47 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2531 / 1.00 <slowest>
I = 199
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1734 / 1.48 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2563 / 1.00 <slowest>
I = 299
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1672 / 1.51 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2531 / 1.00 <slowest>
I = 399
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1671 / 1.48 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2469 / 1.00 <slowest>
I = 499
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1671 / 1.52 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2532 / 1.00 <slowest>
I = 599
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1687 / 1.46 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2469 / 1.00 <slowest>
I = 699
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1672 / 1.52 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2546 / 1.00 <slowest>
I = 799
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1703 / 1.45 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2469 / 1.00 <slowest>
I = 899
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1688 / 1.49 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2516 / 1.00 <slowest>
I = 999
Elapsed milliseconds / relative speed for 2048 iteration(s):
(REPLACE_CAB_OPTIM *LIST@ I "A").....1671 / 1.50 <fastest>
(REPLACE_CAB *LIST@ I "A")...........2500 / 1.00 <slowest>
1099
Command:
Has that been your experience?
(setq my-lst '(1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0))
(benchmark
'(
( (lambda ( lst )
(mapcar
(function
1+) lst))
my-lst)
( (lambda ( lst )
(mapcar '1+ lst))
my-lst)
)
)
Elapsed milliseconds / relative speed for 8192 iteration(s):
((LAMBDA (LST) (MAPCAR (FUNCTION 1+)...).....1031 / 1.05 <fastest>
((LAMBDA (LST) (MAPCAR (QUOTE 1+) LS...).....1078 / 1.00 <slowest>
Command: <Osnap off>
Command: (benchmark
(_> '(
('(_> ( (lambda ( lst )
('(((_> (mapcar
('((((_> (function
('(((((_> 1+) lst))
('((_> my-lst)
('(_> ( (lambda ( lst )
('(((_> (mapcar '1+ lst))
('((_> my-lst)
('(_>
('(_>
('(_> )
(_> )
Elapsed milliseconds / relative speed for 8192 iteration(s):
((LAMBDA (LST) (MAPCAR (FUNCTION 1+)...).....1015 / 1.06 <fastest>
((LAMBDA (LST) (MAPCAR (QUOTE 1+) LS...).....1078 / 1.00 <slowest>
Command: (benchmark
(_> '(
('(_> ( (lambda ( lst )
('(((_> (mapcar
('((((_> (function
('(((((_> 1+) lst))
('((_> my-lst)
('(_> ( (lambda ( lst )
('(((_> (mapcar '1+ lst))
('((_> my-lst)
('(_> )
(_> )
Elapsed milliseconds / relative speed for 8192 iteration(s):
((LAMBDA (LST) (MAPCAR (FUNCTION 1+)...).....1031 / 1.03 <fastest>
((LAMBDA (LST) (MAPCAR (QUOTE 1+) LS...).....1062 / 1.00 <slowest>
(progn
(defun add-one-to-items ( lst )
(mapcar (function 1+) lst))
(defun add-one-to-items--ver2 ( lst )
(
(lambda ( lst )
(mapcar (function 1+) lst))
lst))
(defun add-one-to-items--ver3 ( lst )
(mapcar '1+ lst))
(defun add-one-to-items--ver4 ( lst )
(
(lambda ( lst )
(mapcar '1+ lst))
lst))
(setq my-lst '(1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0))
(benchmark
'(
(add-one-to-items my-lst)
(add-one-to-items--ver2 my-lst)
(add-one-to-items--ver3 my-lst)
(add-one-to-items--ver4 my-lst)
)
)
)
Elapsed milliseconds / relative speed for 16384 iteration(s):
(ADD-ONE-TO-ITEMS--VER3 MY-LST).....1719 / 1.03 <fastest>
(ADD-ONE-TO-ITEMS--VER4 MY-LST).....1719 / 1.03
(ADD-ONE-TO-ITEMS--VER2 MY-LST).....1750 / 1.01
(ADD-ONE-TO-ITEMS MY-LST)...........1765 / 1.00 <slowest>
Elapsed milliseconds / relative speed for 16384 iteration(s):
(ADD-ONE-TO-ITEMS--VER2 MY-LST).....1750 / 1.01 <fastest>
(ADD-ONE-TO-ITEMS--VER3 MY-LST).....1750 / 1.01
(ADD-ONE-TO-ITEMS--VER4 MY-LST).....1750 / 1.01
(ADD-ONE-TO-ITEMS MY-LST)...........1766 / 1.00 <slowest>
2 & 4 should be grouped together or 1 & 3 should... ?!I = 50
Benchmarking ........ done for 8192 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(REPLASE_1 *LIST@ I A) 8192 1170 1170 13.01
(IB:REPLACE-NTH *LIST@ I A) 8192 1248 1248 12.20
(REPLACE_CAB *LIST@ I A) 2048 1965 7860 1.94
(REPLASE *LIST@ I A) 1024 1077 8616 1.77
(SWAPNTH *LIST@ I A) 1024 1325 10600 1.44
(REPLASE_2 *LIST@ I A) 1024 1732 13856 1.10
(REPLACE_DA A I *LIST@) 1024 1842 14736 1.03
(NTH-REPLACE I A *LIST@) 1024 1903 15224 1.00
--------------------------------------------------------------------------------
I = 150
Benchmarking ........ done for 4096 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A) 4096 1170 1170 8.43
(REPLASE_1 *LIST@ I A) 4096 1545 1545 6.38
(REPLACE_CAB *LIST@ I A) 1024 1217 4868 2.03
(REPLASE *LIST@ I A) 1024 1325 5300 1.86
(REPLASE_2 *LIST@ I A) 1024 1762 7048 1.40
(SWAPNTH *LIST@ I A) 1024 1888 7552 1.31
(REPLACE_DA A I *LIST@) 512 1202 9616 1.03
(NTH-REPLACE I A *LIST@) 512 1233 9864 1.00
--------------------------------------------------------------------------------
I = 250
Benchmarking ........ done for 4096 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A) 4096 1653 1653 5.96
(REPLASE_1 *LIST@ I A) 2048 1232 2464 4.00
(REPLACE_CAB *LIST@ I A) 1024 1201 4804 2.05
(REPLASE *LIST@ I A) 1024 1341 5364 1.84
(REPLASE_2 *LIST@ I A) 1024 1733 6932 1.42
(SWAPNTH *LIST@ I A) 512 1076 8608 1.14
(NTH-REPLACE I A *LIST@) 512 1219 9752 1.01
(REPLACE_DA A I *LIST@) 512 1232 9856 1.00
--------------------------------------------------------------------------------
I = 350
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A) 2048 1014 1014 4.99
(REPLASE_1 *LIST@ I A) 2048 1700 1700 2.98
(REPLACE_CAB *LIST@ I A) 1024 1217 2434 2.08
(REPLASE *LIST@ I A) 1024 1357 2714 1.86
(REPLASE_2 *LIST@ I A) 1024 1762 3524 1.44
(SWAPNTH *LIST@ I A) 512 1186 4744 1.07
(REPLACE_DA A I *LIST@) 512 1232 4928 1.03
(NTH-REPLACE I A *LIST@) 512 1265 5060 1.00
--------------------------------------------------------------------------------
I = 450
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A) 2048 1184 1184 4.58
(REPLASE_1 *LIST@ I A) 1024 1061 2122 2.56
(REPLACE_CAB *LIST@ I A) 1024 1201 2402 2.26
(REPLASE *LIST@ I A) 1024 1342 2684 2.02
(REPLASE_2 *LIST@ I A) 1024 1794 3588 1.51
(REPLACE_DA A I *LIST@) 512 1186 4744 1.14
(NTH-REPLACE I A *LIST@) 512 1218 4872 1.11
(SWAPNTH *LIST@ I A) 512 1357 5428 1.00
--------------------------------------------------------------------------------
I = 550
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A) 2048 1436 1436 4.26
(REPLACE_CAB *LIST@ I A) 1024 1200 2400 2.55
(REPLASE_1 *LIST@ I A) 1024 1264 2528 2.42
(REPLASE *LIST@ I A) 1024 1340 2680 2.28
(REPLASE_2 *LIST@ I A) 1024 1748 3496 1.75
(REPLACE_DA A I *LIST@) 512 1233 4932 1.24
(NTH-REPLACE I A *LIST@) 512 1248 4992 1.23
(SWAPNTH *LIST@ I A) 512 1529 6116 1.00
--------------------------------------------------------------------------------
I = 650
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A) 2048 1653 1653 4.23
(REPLACE_CAB *LIST@ I A) 1024 1232 2464 2.84
(REPLASE *LIST@ I A) 1024 1310 2620 2.67
(REPLASE_1 *LIST@ I A) 1024 1514 3028 2.31
(REPLASE_2 *LIST@ I A) 1024 1764 3528 1.98
(REPLACE_DA A I *LIST@) 512 1232 4928 1.42
(NTH-REPLACE I A *LIST@) 512 1232 4928 1.42
(SWAPNTH *LIST@ I A) 512 1747 6988 1.00
--------------------------------------------------------------------------------
I = 750
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A) 2048 1888 1888 4.17
(REPLACE_CAB *LIST@ I A) 1024 1233 2466 3.19
(REPLASE *LIST@ I A) 1024 1358 2716 2.90
(REPLASE_1 *LIST@ I A) 1024 1717 3434 2.29
(REPLASE_2 *LIST@ I A) 1024 1733 3466 2.27
(NTH-REPLACE I A *LIST@) 512 1247 4988 1.58
(REPLACE_DA A I *LIST@) 512 1248 4992 1.58
(SWAPNTH *LIST@ I A) 512 1966 7864 1.00
--------------------------------------------------------------------------------
I = 850
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A) 2048 2029 2029 4.37
(REPLACE_CAB *LIST@ I A) 1024 1201 2402 3.69
(REPLASE *LIST@ I A) 1024 1310 2620 3.38
(REPLASE_2 *LIST@ I A) 1024 1794 3588 2.47
(REPLASE_1 *LIST@ I A) 1024 1965 3930 2.26
(REPLACE_DA A I *LIST@) 512 1217 4868 1.82
(NTH-REPLACE I A *LIST@) 512 1248 4992 1.78
(SWAPNTH *LIST@ I A) 256 1108 8864 1.00
--------------------------------------------------------------------------------
I = 950
Benchmarking ........ done for 1024 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A) 1024 1155 1155 4.32
(REPLACE_CAB *LIST@ I A) 1024 1217 1217 4.10
(REPLASE *LIST@ I A) 1024 1295 1295 3.85
(REPLASE_2 *LIST@ I A) 1024 1778 1778 2.81
(REPLASE_1 *LIST@ I A) 512 1092 2184 2.29
(NTH-REPLACE I A *LIST@) 512 1183 2366 2.11
(REPLACE_DA A I *LIST@) 512 1233 2466 2.02
(SWAPNTH *LIST@ I A) 256 1248 4992 1.00
--------------------------------------------------------------------------------1050
_$
Anyhow, the principle of mine was actually from an algorithm I saw Elpanov Evgeniy use in another routine.
;; written by qj-chen
;; Edited by GSLS(SS)
(defun ch-lst (new i lst / j len fst mid)
(if (/= (type i) (quote list))
(cond
((not (listp lst))
lst)
((minusp i)
lst
)
((> i (setq len (length lst)))
lst
)
((> i (/ len 2))
(reverse (ch-lst new (1- (- len i)) (reverse lst)))
)
(t
(append
(progn
(setq fst nil)
(repeat (rem i 4)
(setq fst (cons (car lst) fst)
lst (cdr lst)
)
)
(repeat (/ i 4)
(setq fst (cons (cadddr lst)
(cons (caddr lst)
(cons
(cadr lst)
(cons
(car lst)
fst
)
)
)
)
lst (cddddr lst)
)
)
(reverse fst)
)
(list new)
(cdr lst)
)
)
)
(progn
(setq j (cadr i)
i (car i)
)
(if j
(progn
(setq mid (nth i lst))
(setq mid (ch-lst new j mid))
(ch-lst mid i lst)
)
(ch-lst new i lst)
)
)
)
)
E.g. (ch-lst 3 2 '(1 1 1 1 1 1 1)) --> '(1 1 3 1 1 1 1)
(ch-lst 3 '(2 2) '((1 1 1 1 1 1 ) (1 1 1 1 1 1 ) (1 1 1 1 1 1) (1 1 1 1 1 1) (1 1 1 1 1 1) (1 1 1 1 1 1) ))
-->' ((1 1 1 1 1 1) (1 1 1 1 1 1) (1 1 3 1 1 1) (1 1 1 1 1 1) (1 1 1 1 1 1) (1 1 1 1 1 1))
Remove nth (defun remove-nth (i lst / j len fst)
(if (/= (type i) (quote list))
(cond
((or (minusp i) (> i (1- (setq len (length lst)))))
lst
)
((> i (/ len 2))
(reverse (remove-nth (1- (- len i)) (reverse lst)))
)
(t
(append
(progn
(setq fst nil)
(repeat (rem i 4)
(setq fst (cons (car lst) fst)
lst (cdr lst)
)
)
(repeat (/ i 4)
(setq fst (cons (cadddr lst)
(cons (caddr lst)
(cons
(cadr lst)
(cons
(car lst)
fst
)
)
)
)
lst (cddddr lst)
)
)
(reverse fst)
)
(cdr lst)
)
)
)
(progn
(setq j (cadr i)
i (car i)
)
(if j
(mapcar (function (lambda (x) (remove-nth j x))) (remove-nth i lst))
(remove-nth i lst)
)
)
)
)
e.g. (remove-nth 2 '(1 1 3 1 1 1 1)) --> '(1 1 1 1 1 1)
(remove-nth '(2 2) ' ((1 1 1 1 1 1) (1 1 1 1 1 1) (1 1 3 1 1 1) (1 1 1 1 1 1) (1 1 1 1 1 1) (1 1 1 1 1 1)))
-->((1 1 1 1 1) (1 1 1 1 1) (1 1 1 1 1) (1 1 1 1 1) (1 1 1 1 1))
@Irneb, for a little extra concision, this:Code - Auto/Visual Lisp: [Select]
could become:Code - Auto/Visual Lisp: [Select]I'm unsure of any performance benefit however.
...
Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):
(VL-LIST*-IT L).....1107 / 1.25 <fastest>
(CONS-IT L).........1389 / 1.00 <slowest>[/code]
Nice method , Lee .
Thanks Evgeniy,
You can now see how the position of the index in the list affects the time required.
Also the compiling surprised me as I thought it would be a uniform change in the speed & not the order.
Funny how some methods fair better when compiled.
I wish to give you a small gift by new year... :-) :-) :-)
:-)
I congratulate on coming new year!
Let new year will bring a lot of pleasure and happiness!
Happy New Year!
:-)
Code - Auto/Visual Lisp: [Select]
Sorry to revive this ancient thread, just figured my version seems to outperform all the others in nearly every instance
(defun ALE_List_SubstNth (NthPos NewItm In_Lst InRLst / LstLng OldItm)
(cond
( (null In_Lst) nil )
( (zerop NthPos) (cons NewItm (cdr In_Lst)) )
( (<= (setq LstLng (length In_Lst)) NthPos) In_Lst )
( (zerop (setq LstLng (- LstLng (1+ NthPos))))
(append (reverse (cdr InRLst)) (list NewItm))
)
( T
(setq OldItm (nth NthPos In_Lst))
(while
(/=
NthPos
(length (setq InRLst (cdr (member OldItm InRLst))))
)
)
(while
(/=
LstLng
(length (setq In_Lst (cdr (member OldItm In_Lst))))
)
)
(append (reverse InRLst) (cons NewItm In_Lst))
)
)
)