马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 newer 于 2017-5-16 20:46 编辑
我们知道,vl-position函数只能返回第一个找到元素的索引
比如:
命令: (vl-position 1 '(1 3 2 4 1 4 1))
0
我们自己写个函数 vl-positions , 能返回多个索引,比如上面的:
命令: (vl-positions 1 '(1 3 2 4 1 4 1))
(0 4 6)
抛砖引玉,我来一个:
 - (defun vl-positions1 ( x l / i )
- (setq i -1)
- (vl-remove nil (mapcar '(lambda ( y ) (setq i (1+ i)) (if (= x y) i)) l))
- )
_$ (vl-positions1 1 '(1 3 2 4 1 4 1))
(0 4 6)
_$
附测试效率代码(xd::benchmark函数,需加载晓东通用LISP函数库2017.0518+版本):
加入vl-positions11测试结果
- 命令: (repeat 5 (c:test))
- Elapsed milliseconds / relative speed for 32768 iteration(s):
- (VL-POSITIONS7 1 LST)......1437 / 2.76 <fastest>
- (VL-POSITIONS9 1 LST)......1609 / 2.47
- (VL-POSITIONS6 1 LST)......1625 / 2.44
- (VL-POSITIONS10 1 LST).....1671 / 2.38
- (VL-POSITIONS8 1 LST)......1859 / 2.14
- (VL-POSITIONS1 1 LST)......1938 / 2.05
- (VL-POSITIONS2 1 LST)......1968 / 2.02
- (VL-POSITIONS4 1 LST)......1984 / 2
- (VL-POSITIONS5 1 LST)......2156 / 1.84
- (VL-POSITIONS11 1 LST).....3969 / 1 <slowest>
- Elapsed milliseconds / relative speed for 32768 iteration(s):
- (VL-POSITIONS6 1 LST).......1985 / 8.45 <fastest>
- (VL-POSITIONS8 1 LST).......2172 / 7.72
- (VL-POSITIONS7 1 LST).......2250 / 7.45
- (VL-POSITIONS4 1 LST).......2406 / 6.97
- (VL-POSITIONS2 1 LST).......2860 / 5.86
- (VL-POSITIONS5 1 LST).......2938 / 5.71
- (VL-POSITIONS1 1 LST).......3047 / 5.5
- (VL-POSITIONS9 1 LST).......3703 / 4.53
- (VL-POSITIONS10 1 LST)......6687 / 2.51
- (VL-POSITIONS11 1 LST).....16765 / 1 <slowest>
- Elapsed milliseconds / relative speed for 32768 iteration(s):
- (VL-POSITIONS6 1 LST)......1735 / 2.22 <fastest>
- (VL-POSITIONS8 1 LST)......1906 / 2.02
- (VL-POSITIONS9 1 LST)......2000 / 1.92
- (VL-POSITIONS7 1 LST)......2016 / 1.91
- (VL-POSITIONS10 1 LST).....2203 / 1.74
- (VL-POSITIONS4 1 LST)......2297 / 1.67
- (VL-POSITIONS5 1 LST)......2407 / 1.6
- (VL-POSITIONS2 1 LST)......2672 / 1.44
- (VL-POSITIONS1 1 LST)......2766 / 1.39
- (VL-POSITIONS11 1 LST).....3844 / 1 <slowest>
- Elapsed milliseconds / relative speed for 32768 iteration(s):
- (VL-POSITIONS7 1 LST)......1375 / 2.64 <fastest>
- (VL-POSITIONS8 1 LST)......1406 / 2.58
- (VL-POSITIONS9 1 LST)......1531 / 2.37
- (VL-POSITIONS6 1 LST)......1594 / 2.27
- (VL-POSITIONS4 1 LST)......2000 / 1.81
- (VL-POSITIONS10 1 LST).....2016 / 1.8
- (VL-POSITIONS5 1 LST)......2500 / 1.45
- (VL-POSITIONS2 1 LST)......2531 / 1.43
- (VL-POSITIONS1 1 LST)......2859 / 1.27
- (VL-POSITIONS11 1 LST).....3625 / 1 <slowest>
- Elapsed milliseconds / relative speed for 32768 iteration(s):
- (VL-POSITIONS7 1 LST)......1343 / 2.62 <fastest>
- (VL-POSITIONS8 1 LST)......1469 / 2.39
- (VL-POSITIONS6 1 LST)......1547 / 2.27
- (VL-POSITIONS9 1 LST)......1687 / 2.08
- (VL-POSITIONS4 1 LST)......1984 / 1.77
- (VL-POSITIONS1 1 LST)......2047 / 1.72
- (VL-POSITIONS10 1 LST).....2219 / 1.58
- (VL-POSITIONS2 1 LST)......2250 / 1.56
- (VL-POSITIONS5 1 LST)......2281 / 1.54
- (VL-POSITIONS11 1 LST).....3516 / 1 <slowest>
复制代码
目前,wowan1314的 vl-positions7 表现最好
|