向列表的每个元素添加一个的哪个函数更快?
Which function for adding one to every element of a list is faster?
我想知道以下哪个函数的计算复杂度较低。
背景:我有一个触发信号,该信号触发一个规则,该规则调用我的函数,该函数将 1 添加到特定列表的每个元素。因为该触发信号发送得非常频繁,所以我很感兴趣我应该更喜欢以下哪些功能。
函数 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)
)
函数 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)
)
我建议根据经验进行测试:
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>
我想知道以下哪个函数的计算复杂度较低。 背景:我有一个触发信号,该信号触发一个规则,该规则调用我的函数,该函数将 1 添加到特定列表的每个元素。因为该触发信号发送得非常频繁,所以我很感兴趣我应该更喜欢以下哪些功能。 函数 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)
)
函数 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)
)
我建议根据经验进行测试:
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>