马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本次测试优化了while repeat 和mapcar的算法,测试结果还是不令人满意!
测试数据为:660个边数为1000的多边形,分别用while、repeat、foreach、mapcar,将角点放入一个表中!
while耗时为 4.73秒 repeat 耗时为 4.80秒 foreach 耗时为 90.23秒 mapcar 耗时为 5.53秒
存在两个小问题。
1、用foreach函数怎么过滤掉非角点子表,否则foreach算法方面不如其他函数,以至于测试结果耗时90秒!!!
2、测试是不是要在程序编译的情况下进行呢?我是直接加载代码测试的,不知对否。
![](source/plugin/imc_colorcode/images/loading.gif) - ;测试思路:求正多边形的角点,660个边数为1000的多边形,分别用while、repeat、foreach、mapcar,将角点放入一个表中!
- ;一、while
- (defun c:111( / lst zm) ;test_while
- (setq ss (ssget '((0 . "LWPOLYLINE"))) i 0 lst nil time1 (get-utime))
- (while (< i (sslength ss))
- (setq zm (entget (ssname ss i)))
- (while (assoc 10 zm)
- (setq lst (cons (cdr (assoc 10 zm)) lst) zm (cdr (member (assoc 10 zm) zm)));不能用member函数,算法很好,不公平
- )
- (setq i (+ i 1))
- )
- (setq time2 (get-utime) time (strcat "消耗时间为:"(rtos (- time2 time1)) "秒"))
- )
- ;二、repeat
- (defun c:222( / i j lst num ss zm);test_repeat
- (setq ss (ssget '((0 . "LWPOLYLINE"))) i 0 lst nil num (cdr (assoc 90 (entget (ssname ss 0)))) time1 (get-utime))
- (repeat (sslength ss)
- (setq zm (entget (ssname ss i)) j 0)
- (repeat num
- (setq lst (cons (cdr (assoc 10 zm)) lst) zm (cdr (member (assoc 10 zm) zm)));不能用member函数,算法很好,不公平
- (setq j (+ j 1))
- )
- (setq i (+ i 1))
- )
- (setq time2 (get-utime) time (strcat "消耗时间为:"(rtos (- time2 time1)) "秒"))
- )
- ;三、foreach
- (defun c:333( / ent ents i li lst ss zm);test_foreach
- (setq ss (ssget '((0 . "LWPOLYLINE"))) i 0 lst nil zm nil time1 (get-utime))
- (setq ents (cpf-ss->ents ss))
- (foreach ent ents (if (not zm) (setq zm (entget ent)) (setq zm (append (entget ent) zm))));这里只能用append
- (foreach li zm (if (= (car li) 10) (setq lst (cons (cdr li) lst))))
- (setq time2 (get-utime) time (strcat "消耗时间为:"(rtos (- time2 time1)) "秒"))
- )
- ;四、mapcar
- (defun c:444( / ents lst ss x zmlst);test_mapcar
- (setq ss (ssget '((0 . "LWPOLYLINE"))) lst nil zmlst nil time1 (get-utime))
- (setq ents (cpf-ss->ents ss))
- (setq zmlst (apply 'append (mapcar '(lambda(x) (entget x)) ents)))
- (setq lst_10 (vl-remove-if-not '(lambda(x) (= (car x) 10)) zmlst))
- (setq lst (mapcar '(lambda(x) (cdr x)) lst_10))
- (setq time2 (get-utime) time (strcat "消耗时间为:"(rtos (- time2 time1)) "秒"))
- )
- ;子函数,选择集转化为图元表(通用函数)
- (defun cpf-ss->ents(ss / ents i)
- (setq i 0)
- (repeat (sslength ss)
- (setq ents (cons (ssname ss i) ents) i (1+ i))
- )
- ents
- )
- ;;测试时间函数(通用函数)
- (defun get-utime ()
- (* 86400.0 (getvar "tdusrtimer"))
- )
|