明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2543|回复: 5

[讨论] 实践检验cons与append生成lisp的耗时差矣

  [复制链接]
发表于 2013-12-31 15:47 | 显示全部楼层 |阅读模式
本帖最后由 林霄云 于 2017-8-7 09:48 编辑

一个疑惑或成常识
大家写的常见的代码,包括cad帮助里的示例,在形成列表时,都是采用cons然后反转的思路。天生的append函数为何不用呢?为了弄清白,在q3_2006提供的耗时函数下,我做了如下测试:
  1. (defun vtime ()
  2.   (* 86400 (getvar "tdusrtimer"))
  3. )

  4. (setq test-time nil)
  5. (setq t0 (vtime));设置起点
  6. ;代码块
  7. (setq n 1000)
  8. (setq i 0)
  9. (repeat n
  10.   (setq i(1+ i))
  11. (setq test-time (append (list i)  test-time))
  12. );n放大系数
  13. ;(princ test-time)
  14. ;代码块
  15. (princ "\nappend")
  16. (princ (strcat "\n耗时" (rtos (- (vtime) t0) 2 3) "秒"));设置终点

  17. (setq test-time nil)
  18. (setq t0 (vtime));设置起点
  19. ;代码块
  20. (setq n 1000)
  21. (setq i 0)
  22. (repeat n
  23.   (setq i(1+ i))
  24. (setq test-time (cons i  test-time))
  25. );n放大系数
  26. (setq test-time(reverse test-time))

  27. ;(princ test-time)
  28. ;代码块
  29. (princ "\ncons and reverse")
  30. (princ (strcat "\n耗时" (rtos (- (vtime) t0) 2 3) "秒"));设置终点
结果如下
append
耗时0.047秒
cons and reverse
耗时0.015秒

append
耗时0.031秒
cons and reverse
耗时0.000秒

append
耗时0.032秒
cons and reverse
耗时0.000秒

初步结论:cons是对atom级别的操作,append是list级别的操作,cons and reverse和append效率不在一个数量集上。应该尽量使用cons and reverse。


该贴已经同步到 林霄云的微博

点评

受教,有时候还有脱括号的问题,还得用append  发表于 2017-8-3 14:25
"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2013-12-31 17:22 | 显示全部楼层
不错,虽然我经常用cons少用append,但还真没注意这个问题
发表于 2013-12-31 22:45 | 显示全部楼层
空了试试vla-put-layer和entmod方式才速度比。。
发表于 2017-7-26 15:38 | 显示全部楼层
这是我自己的分析,你看看我说的对不对
http://bbs.ivlisp.com/forum.php?mod=viewthread&tid=35&extra=
我觉得应该是这个原因
发表于 2017-7-26 16:42 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-3 01:26 , Processed in 0.441818 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表