I'm wondering which of the following function is less computational complex. The background: I have a trigger signal and that signal triggers a rule that invokes my function which adds a 1 to every element of a certain list. Because that trigger signal is send quite frequently I'm interested which of the following functions I should prefer. Function 1:
;?lon = list of numbers
(deffunction add-one-to-list-of-numbers (?lon)
(progn$ (?field ?lon)
(bind ?lon (replace$ ?lon ?field-index ?field-index (+ ?field 1)))
)
(return ?lon)
)
Function 2:
;?lon = list of numbers
;?cnt = counter
(deffunction add-one-to-list-of-numbers-alt (?lon)
(loop-for-count (?cnt (length ?lon))
(bind ?lon (replace$ ?lon ?cnt ?cnt (+(nth$ ?cnt ?lon) 1)))
)
(return ?lon)
)
I'd suggest testing empirically:
CLIPS (6.31 2/3/18)
CLIPS>
(deffunction add-one-to-list-of-numbers-1 (?lon)
(progn$ (?field ?lon)
(bind ?lon (replace$ ?lon ?field-index ?field-index (+ ?field 1))))
(return ?lon))
CLIPS>
(deffunction add-one-to-list-of-numbers-2 (?lon)
(loop-for-count (?cnt (length$ ?lon))
(bind ?lon (replace$ ?lon ?cnt ?cnt (+(nth$ ?cnt ?lon) 1))))
(return ?lon))
CLIPS>
(deffunction add-one-to-list-of-numbers-3 (?lon)
(bind ?rv (create$))
(progn$ (?field ?lon)
(bind ?rv (create$ ?rv (+ ?field 1))))
?rv)
CLIPS>
(timer (bind ?numbers (create$ 1 2 3 4 5 6 7 8 9))
(loop-for-count 1000000 (add-one-to-list-of-numbers-1 ?numbers)))
7.51635100000021
CLIPS> (release-mem)
13499
CLIPS>
(timer (bind ?numbers (create$ 1 2 3 4 5 6 7 8 9))
(loop-for-count 1000000 (add-one-to-list-of-numbers-2 ?numbers)))
9.28229099999953
CLIPS> (release-mem)
3771
CLIPS>
(timer (bind ?numbers (create$ 1 2 3 4 5 6 7 8 9))
(loop-for-count 1000000 (add-one-to-list-of-numbers-3 ?numbers)))
6.42367899999954
CLIPS>