马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
More and more of you are finding that you have to deal with this goofy language called AutoLISP. Here are a couple items I pulled together related to some lisp “best practices”. Both apply to optimizing performance when processing large “lists” of data. Thanks, Nate.
CONS versus APPEND The cons function adds an element to the beginning of a list. The append function can be used to give the equivalent of adding an element to the beginning or end of a list. Using the cons function can be MUCH faster than using append to build up a large list. We'll run two tests to create a dummy list of 10,000 integer numbers. The first test is using the "cons" function. Appload and type test1 [enter] at the command prompt. [pcode=lisp,true](defun c:test1 ( / )
(setq i 1)
(setq lst nil) ; start out with a blank list
(repeat 10000
(setq lst (cons i lst)) ; add next element to beginning of list
(setq i (1+ i))
)
(setq lst (reverse lst)) ; put list back into correct order
(princ)
)[/pcode]The second test yields the same result but uses the "append" function: [pcode=lisp,true](defun c:test2 ( / )
(setq i 1)
(setq lst nil) ; start out with a blank list
(repeat 10000
(setq lst (append lst (list i))) ; append next element on to end of list
(setq i (1+ i))
)
(princ)
)[/pcode]The first test using "cons" builds the 10,000 element list in memory in less than 0.01 seconds (on my T61p).The second test using "append" builds the exact same 10,000 element list in memory but takes a full 3.55 seconds to execute ( ! ). Dealing with large lists, it appears that the "cons" function is many, many times faster. 构造10,000元素表,Cons 用时 0.01 , append 用时 3.55 FOREACH versus WHILE / NTH Let's say you need to cycle through a huge list, one list element at a time. There are two different functions that can cycle through a list, "foreach" and "nth" combined with a "while" loop. When dealing with a very large list, the "foreach" function can be much faster than using a while loop / nth function to index through the list. These tests use the 10,000 element list held in memory created be either of the above two tests. This next test uses "foreach" to cycle through the 10,000 element list. [pcode=lisp,true](defun c:test3 ( / )
; use 10,000 element "lst" created by test1 or test2
(setq find_int (getint "\nFind integer="))
(setq foundit nil)
(foreach x lst
(if (AND (not foundit) (= x find_int))
(progn
(setq foundit T)
(princ " found")
) )
)
(princ)
)[/pcode]This next test does the same thing but uses a "while" loop and the "nth" function to index its way through the 10,000 element list: [pcode=lisp,true](defun c:test4 ( / )
; use 10,000 element "lst" created by test1 or test2
(setq find_int (getint "\nFind integer="))
(setq foundit nil)
(setq ix 0)
(setq slen (length lst))
(while (AND (not foundit)(< ix slen))
(if (= (nth ix lst) find_int) ; look for match
(progn ; Found the target element
(setq foundit T)
(princ " found")
) )
(setq ix (1+ ix))
)
(princ)
)[/pcode]For the test, looking for integer value 5000 (halfway into the list). The " foreach" function finds and exits in less than 0.01 second. The while loop using the "nth" function finds and exits in 0.07 seconds. Using foreach is significantly faster in processing this large list.
原文地址: http://www.cppblog.com/85940806/articles/66489.html |