本帖最后由 Gu_xl 于 2014-4-8 23:04 编辑
tt1和tt4慢的原因并不在于是repeat 和while,而是在与其用了nth函数!将TT1和TT4修改一下,不用nth,速度极大提高:
 - (defun c:tt1 (/ s seconds lst mylst i s2 seconds2)
- (setq s (getvar "DATE"))
- (setq seconds (* 86400.0 (- s (fix s))))
- (setq lst (ATOMS-FAMILY 1))
- (setq n(length lst) lst1 lst)
- (repeat 100
- (setq i 0
- mylst nil
- lst1 lst
- )
- (repeat n
- ;(setq thisitm (nth i lst)
- ; i (1+ i)
- ;)
- (setq thisitm (car lst1) lst1 (cdr lst1))
- (if (= "A" (substr thisitm 1 1))
- (setq mylst (cons thisitm mylst))
- )
- )
- )
- (setq mylst (REVERSE ylst));_这行对速度基本没影响
- (setq s2 (getvar "DATE"))
- (setq seconds2 (* 86400.0 (- s2 (fix s2))))
- (princ
- (strcat "\n tt1 运行时间:" (rtos (- seconds2 seconds) 2 2) " 秒")
- )
- )
- (defun c:tt4 (/ i len s seconds lst mylst itm s2 seconds2)
- (setq s (getvar "DATE"))
- (setq seconds (* 86400.0 (- s (fix s))))
- (setq lst (ATOMS-FAMILY 1))
- (setq len(length lst) )
- (repeat 100
- (setq mylst nil i 0 lst1 lst)
-
- (while (< i len)
- ;(setq itm(nth i lst))
- (setq itm (car lst1) lst1 (cdr lst1))
- (if (= "A" (substr itm 1 1))
- (setq mylst (cons itm mylst))
- )
- (setq i(1+ i))
- )
- )
- (setq mylst (REVERSE mylst));_这行对速度基本没影响
- (setq s2 (getvar "DATE"))
- (setq seconds2 (* 86400.0 (- s2 (fix s2))))
- (princ
- (strcat "\ntt3 运行时间:" (rtos (- seconds2 seconds) 2 2) " 秒")
- )
- )
还可以使用mapcar函数来测试:
 - (defun c:tt5 (/ i len s seconds lst mylst itm s2 seconds2)
- (setq s (getvar "DATE"))
- (setq seconds (* 86400.0 (- s (fix s))))
- (setq lst (ATOMS-FAMILY 1))
- (setq len(length lst) )
- (repeat 100
- (setq mylst nil i 0 )
- (mapcar '(lambda (itm)
- (if (= "A" (substr itm 1 1))
- (setq mylst (cons itm mylst))
- )
- )
- lst)
- )
- (setq mylst (REVERSE mylst));_这行对速度基本没影响
- (setq s2 (getvar "DATE"))
- (setq seconds2 (* 86400.0 (- s2 (fix s2))))
- (princ
- (strcat "\ntt3 运行时间:" (rtos (- seconds2 seconds) 2 2) " 秒")
- )
- )
修改后上述几个程序编译后运行,速度相差就不那么明显了:
tt1 运行时间:1.58 秒
tt2 运行时间:1.33 秒
tt3 运行时间:1.29 秒
tt4 运行时间:1.91 秒
tt5 运行时间:1.39 秒 |