本帖最后由 highflybird 于 2022-7-13 02:14 编辑
起源于baitang36的这个帖子 :
给autolisp增加一种数据类型vector
我把vector和list两种数据类型的读取元素的速度进行了比较。
测试代码:
- (defun c:ttt()
- (setq i 0)
- (setq l nil)
- (setq n 100000)
- (repeat n
- (setq l (cons i l))
- (setq i (1+ i))
- )
- (setq v (apply 'vector l))
- ;;最后一个元素
- (uti:bench 10000
- (list
- (list 'vector-elt v (1- n))
- (list 'nth (1- n) l)
- )
- )
- ;;第一个元素
- (uti:bench 10000
- (list
- (list 'vector-elt v 0)
- (list 'nth 0 l)
- )
- )
- ;;中间元素
- (uti:bench 10000
- (list
- (list 'vector-elt v (/ n 2))
- (list 'nth (/ n 2) l)
- )
- )
- (defun forvector (v / i)
- (setq i 0)
- (repeat (vector-length v)
- (vector-elt v i)
- (setq i (1+ i))
- )
- )
- (defun forList (lst / i)
- (setq i 0)
- (repeat (length lst)
- (nth i lst)
- (setq i (1+ i))
- )
- )
-
- ;;平均用时
- (uti:bench 10
- (list
- (list 'forvector v)
- (list 'forlist l)
- )
- )
-
- (princ)
- )
测试函数:
- (defun UTI:Bench (Times Expressions / s)
- (defun Benchmark (Func times / TIME0 TIME1 Speed Value fName)
- (setq fName (car Func))
- (setq TIME0 (getvar "millisecs"))
- (repeat times
- (setq Value (apply fName (cdr func)))
- )
- (setq TIME1 (getvar "millisecs"))
- (setq TIME1 (- TIME1 TIME0 0.0))
- (setq Speed (/ TIME1 times))
- (list fName times TIME1 Speed Value)
- )
- (BenchCommon Times Expressions)
- )
在我的笔记本上测试的结果是:
Statement Times Elapse(ms) Average(ms/time)
------------------------------------------------------------------
VECTOR-ELT 10000 468.0 0.0468
NTH 10000 3063.0 0.3063
Statement Times Elapse(ms) Average(ms/time)
------------------------------------------------------------------
VECTOR-ELT 10000 782.0 0.0782
NTH 10000 828.0 0.0828
Statement Times Elapse(ms) Average(ms/time)
------------------------------------------------------------------
VECTOR-ELT 10000 829.0 0.0829
NTH 10000 2062.0 0.2062
Statement Times Elapse(ms) Average(ms/time)
------------------------------------------------------------------
FORVECTOR 10 4547.0 454.7
FORLIST 10 126953.0 12695.3
读取单个元素的时候,vector优势不是很明显,但平均值优势比较大。
另外vector有一个优点,就是很容易改变某个元素的值或者替换和交换元素,这个是很方便的,对list类型来说,实在不好办。
所以此处我没做比较。
欢迎大家测试。
|