明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1078|回复: 5

[资源] LISP缓存类

[复制链接]
发表于 2018-4-18 16:54 | 显示全部楼层 |阅读模式
缓存类其实是Scripting.Dictionary的LISP封装,可应用于需要加速LISP数据类型的存取的场合。

建立缓存对象
_$ (setq c (p-cache-create))
#<VLA-OBJECT IDictionary 0000000008c8f390>

增加索引为15000的LIST对象
_$ (p-cache-set c "15000" '((10  1200. 1200. 0.)(11  1220. 1220. 0.)(2  "A...")))
nil

根据索引获取对象
_$ (p-cache-get c "15000")
((10 1200.0 1200.0 0.0) (11 1220.0 1220.0 0.0) (2 "A..."))

建立测试数据
(setq n 0)
(repeat        10000
  (p-cache-set c (setq n (1+ n)) (list n n n))
)

获取条目数量
_$ (p-cache-getcount c)
10001

速度评测
_$ (p-benchmark '(p-cache-get c 8888) 10000)
"Benchmark loops = 10000, in 688 ms, 14535 invoke / s"

_$ (p-cache-get c 1)
(1 1 1)

_$ (p-cache-get c 88888)
nil

_$ (p-cache-get c 8888)
(8888 8888 8888)



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2018-4-19 11:11 | 显示全部楼层
本帖最后由 yxp 于 2018-4-19 11:14 编辑

要讲对数据的存取,还是字典法最快,比数组还快,lisp 表最慢。
nth 函数就是从第一个元素开始一个个的问,问到第 n 个后返回;字典法则是直接用关键字保存了第 n 个元素的内存地址,需要的时候直接呼叫。删除重复表元素测试结果:
字典方法耗时: 0.03 秒
递归方法耗时: 1.769 秒
测试代码如下:
  1. ;;LISP递归法
  2. (defun delsame (L)(if L (cons (car L)(delsame (vl-remove (car L) L)))))

  3. ;;字典法
  4. (defun d-DelSame(L / A L1)
  5.         (defun d_LtKI(d k)(mapcar 'vlax-variant-value
  6.                 (vlax-safearray->list (vlax-variant-value (vlax-invoke-method d k))))
  7.         )
  8.     (setq d (vlax-create-object "Scripting.Dictionary"))
  9.     (while (setq A (car L))
  10.         (vlax-put-property d 'Item A "")
  11.         (setq L (cdr L))
  12.     )
  13.     (setq L1 (d_LtKI d "Keys"))
  14.     (vlax-release-object d)
  15. L1
  16. )
  17. ;;测试删除重复表元素,测试表元素个数 6000
  18. (defun test( / AA)
  19.     (setq i 0)
  20.     (while (< i 6000) (setq AA (cons (setq i (1+ i)) AA)))
  21.     (setq time0 (getvar "date")) ;;计时1
  22.     (d-DelSame AA) ;;字典法
  23.     (setq time1 (getvar "date")) ;;计时2
  24.     (delsame AA)  ;;递归法
  25.     (setq time2 (getvar "date")) ;;计时3
  26.     (princ (strcat "\n字典方法耗时: " (rtos (* 86400 (- time1 time0)) 2 4) " 秒"))
  27.     (princ (strcat "\n递归方法耗时: " (rtos (* 86400 (- time2 time1)) 2 4) " 秒"))
  28. (princ)
  29. )

发表于 2018-4-19 12:33 来自手机 | 显示全部楼层
yxp牛,楼主发个函数说明,马上就把代码写好
发表于 2018-4-19 23:34 | 显示全部楼层
我完全看不懂!
发表于 2018-4-20 11:18 | 显示全部楼层
发表于 2018-4-23 10:21 | 显示全部楼层
还是 那句话,楼主,你调用外部对象是需要花费 不少时间的,你的 应该 以 使用次数为测试是根本,否则没有实际意义的 ,可以这么说,单从一次处理大量数据来说来说,command 效率应该是最快的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-19 03:13 , Processed in 0.230813 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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