本帖最后由 yxp 于 2018-4-19 11:14 编辑
要讲对数据的存取,还是字典法最快,比数组还快,lisp 表最慢。
nth 函数就是从第一个元素开始一个个的问,问到第 n 个后返回;字典法则是直接用关键字保存了第 n 个元素的内存地址,需要的时候直接呼叫。删除重复表元素测试结果:
字典方法耗时: 0.03 秒
递归方法耗时: 1.769 秒
测试代码如下:
 - ;;LISP递归法
- (defun delsame (L)(if L (cons (car L)(delsame (vl-remove (car L) L)))))
- ;;字典法
- (defun d-DelSame(L / A L1)
- (defun d_LtKI(d k)(mapcar 'vlax-variant-value
- (vlax-safearray->list (vlax-variant-value (vlax-invoke-method d k))))
- )
- (setq d (vlax-create-object "Scripting.Dictionary"))
- (while (setq A (car L))
- (vlax-put-property d 'Item A "")
- (setq L (cdr L))
- )
- (setq L1 (d_LtKI d "Keys"))
- (vlax-release-object d)
- L1
- )
- ;;测试删除重复表元素,测试表元素个数 6000
- (defun test( / AA)
- (setq i 0)
- (while (< i 6000) (setq AA (cons (setq i (1+ i)) AA)))
- (setq time0 (getvar "date")) ;;计时1
- (d-DelSame AA) ;;字典法
- (setq time1 (getvar "date")) ;;计时2
- (delsame AA) ;;递归法
- (setq time2 (getvar "date")) ;;计时3
- (princ (strcat "\n字典方法耗时: " (rtos (* 86400 (- time1 time0)) 2 4) " 秒"))
- (princ (strcat "\n递归方法耗时: " (rtos (* 86400 (- time2 time1)) 2 4) " 秒"))
- (princ)
- )
|