lanjqka
发表于 2014-1-20 14:56:24
;|
_$$$$$$$$ (3dp->2dp '(0 1 2))
(0 1)
|;
(defun 3dp->2dp (plst)
(if (null (cadr plst))
nil
(list (car plst) (cadr plst))
)
)
;|
_$$$$$$$$ (3dpl->2dpl '((0 1 2)(3 4 5)(6 7 8)))
((0 1) (3 4) (6 7))
|;
(defun 3dpl->2dpl (plst) (mapcar '3dp->2dp plst))
(defun 3dpl->2dpl2 (plst)
(if (null plst)
nil
(cons (3dp->2dp (car plst)) (3dpl->2dpl2 (cdr plst)))
)
)
;| 简单比对一下 3dpl->2dpl 与 3dpl->2dpl2 运行时间 相差不大
_$$$$$$$$ (calrpt 100000 '3DPL->2DPL2)
(repeat 100000 (3DPL->2DPL2 'arglst))
运行时间: 0.639988 s.
_$$$$$$$$ (calrpt 100000 '3DPL->2DPL)
(repeat 100000 (3DPL->2DPL 'arglst))
运行时间: 0.670002 s.
_$$$$$$$$ (calrpt 1000000 '3DPL->2DPL2)
(repeat 1000000 (3DPL->2DPL2 'arglst))
运行时间: 6.364 s.
_$$$$$$$$ (calrpt 1000000 '3DPL->2DPL)
(repeat 1000000 (3DPL->2DPL 'arglst))
运行时间: 6.39602 s.
|;
;|
_$$$$$$$$ (-sub-cons-s '(0 1 2 3 4 5 6) 3)
((0 1 2) (3 4 5))
|;
(defun -sub-cons-s (lst n)
(if (null lst)
nil
(if (< (length lst) n)
nil
(cons (ntha n lst) (-sub-cons-s (nthbc n lst) n))
)
)
)
;|
_$$$$$$$$ (consn 10)
(0 1 2 3 4 5 6 7 8 9 10)
|;
(defun consn (i)
(if (< i 0)
nil
(append (consn (- i 1)) (list i))
)
)
(defun calrpt (n fun / tst tend)
(setq arglist (-sub-cons-s (consn 1001) 3))
(setq tst (getvar "DATE"))
(repeat n (vl-catch-all-apply fun arglst))
(setq tend (getvar "DATE"))
(mapcar 'princ
(list "(repeat "
n
" ("
fun
" '"
"arglst"
"))"
"\n运行时间: "
(* (- tend tst) 86400.0)
" s.\n"
)
)
(princ)
)
lanjqka
发表于 2014-1-21 13:39:03
;|
_$$ (cons0 10)
(1 0 0 0 0 0 0 0 0 0 0)
|;
(defun cons0 (i)
(if (zerop i)
(list 1)
(append (cons0 (- i 1)) (list 0))
)
)
;|
_$$ (Divide (cons0 100) 997)
(0 1003 9027 81243 731193 580742 ...
|;
(defun Divide (xlist d / n v r g a)
(setq n 0
r 0
a nil
)
(repeat (length xlist)
(setq v (+ (nth n xlist) (* r 1000000)))
(setq g (/ v d))
(setq r (rem v d))
(setq a (append a (list g)))
(setq n (1+ n))
)
a
)
;|
_$$ (div! 1 997 600)
(0 1003 9027 81243 731193 580742 ...
|;
(defun div! (n d i / j k)
;|(setq k (ascii "+"))
(defun f1 (str)
(if (= (ascii str) k)
(substr str 2)
(f1 (substr str 2))
)
)
(setq j (atoi (f1 (rtos (/ (expt 2 30) d) 1 0))))
|;
(setq j 6)
(defun f3 (n i)
(if (zerop i)
nil
(cons (/ n d) (f3 (* (rem n d) (expt 10 j)) (- i 1)))
)
)
(f3 n (+ (fix (/ i j)) 2))
)
;| 1/997 = 0.001003009027...
_$$ (calrpt 100000 'Divide '((1 0 0 0 0 0 0 0 0 0 0) 997))
(repeat 100000 (DIVIDE '((1 0 0 0 0 0 0 0 0 0 0) 997)))
运行时间: 2.762 s.
单次运行结果:(0 1003 9027 81243 731193 580742 226680 40120 361083 249749 247743)
_$$ (calrpt 100000 'div! '(1 997 55))
(repeat 100000 (DIV! '(1 997 55)))
运行时间: 1.52898 s.
单次运行结果:(0 1003 9027 81243 731193 580742 226680 40120 361083 249749 247743)
_$$ (calrpt 10000 'Divide (list cons0100 997))
(repeat 10000 (DIVIDE '((1 0 0 0 0 0 0 0 0 0 0 ...
运行时间: 3.541 s.
单次运行结果:(0 1003 9027 81243 731193 ...
_$$ (calrpt 10000 'div! '(1 997 600))
(repeat 10000 (DIV! '(1 997 600)))
运行时间: 1.12299 s.
单次运行结果:(0 1003 9027 81243 731193 ...
|;
(defun calrpt (n fun arglst / tst tend)
(setq tst (getvar "DATE"))
(repeat n (vl-catch-all-apply fun arglst))
(setq tend (getvar "DATE"))
(mapcar 'princ
(list "(repeat "
n
" ("
fun
" '"
arglst
"))"
"\n运行时间: "
(* (- tend tst) 86400.0)
" s.\n单次运行结果:"
)
)
(princ (vl-catch-all-apply fun arglst))
(princ)
)
lanjqka
发表于 2014-1-21 13:48:54
本帖最后由 lanjqka 于 2014-1-21 13:50 编辑
上面的例子
计算1/997高精度结果
对长表进行循环处理 的时间要比 用递归构造长表 的时间 多
特别是表比较长时
q3_2006
发表于 2014-1-30 18:37:17
本帖最后由 q3_2006 于 2014-1-31 07:27 编辑
lanjqka 发表于 2014-1-21 13:48 http://bbs.mjtd.com/static/image/common/back.gif
上面的例子
计算1/997高精度结果
对长表进行循环处理 的时间要比 用递归构造长表 的时间 多
"1.1.1.1"
返回("1.1" "0.1" "0.1")用递归怎么写??拜托指点下,谢谢!
(defun sstr (str n)
(if (= n 0)
nil
(cons (rtos (atof str)) (sstr (substr str (1+ (strlen (rtos (atof str))))) (1- n)))
)
)
(sstr str 3)-->>("1.1" "0.1" "1")
为什么这样....不知道原因呀...???
lanjqka
发表于 2014-2-11 13:33:41
q3_2006 发表于 2014-1-30 18:37 static/image/common/back.gif
"1.1.1.1"
返回("1.1" "0.1" "0.1")用递归怎么写??拜托指点下,谢谢!
是不是这样 在首字符为"."时加一个前导0;|
_$$ (sstr "1.1 .1 .1")
("1.1" "0.1" "0.1")
|;
(defun sstr (str / n)
(setq str (vl-string-trim " " str))
(if (= (ascii str) (ascii "."))
(setq str (strcat "0" str))
)
(setq n (vl-string-search " " str))
(if (= str "")
nil
(cons (substr str 1 n) (if n (sstr (substr str (+ n 1)))))
)
)
q3_2006
发表于 2014-2-11 14:11:51
lanjqka 发表于 2014-2-11 13:33 static/image/common/back.gif
是不是这样 在首字符为"."时加一个前导0
是的是的。。。。谢谢指导!
q3_2006
发表于 2014-2-11 14:13:51
lanjqka 发表于 2014-2-11 13:33 static/image/common/back.gif
是不是这样 在首字符为"."时加一个前导0
也不对。。。。.1前面没有空格的。。。。是拆分数字问题。。。
lanjqka
发表于 2014-2-11 16:30:28
本帖最后由 lanjqka 于 2014-2-11 16:34 编辑
应该是 rtos 的原因
(rtos number ]);|
_$$$$ (sstr "1.1.1.1" 3)
("1.1" "0.1" "0.1")
|;
(defun sstr (str n / tmp)
(if (zerop n)
nil
(append (list (setq tmp (rtos (atof str) 2 1)))
(sstr (strcat "0" (substr str (+ (strlen tmp) 1))) (- n 1))
)
)
)
lanjqka
发表于 2014-2-11 17:01:20
q3_2006 发表于 2014-2-11 14:13 static/image/common/back.gif
也不对。。。。.1前面没有空格的。。。。是拆分数字问题。。。
;|
_$$ (sstr "1.1.1.1")
("1.1" "0.1" "0.1")
|;
(defun sstr (str / n)
(setq str (vl-string-trim " " str))
(if (= (ascii str) (ascii "."))
(setq str (strcat "0" str))
)
(setq n (vl-string-search "." str 2))
(if (= str "")
nil
(cons (substr str 1 n) (if n (sstr (substr str (+ n 1)))))
)
)
q3_2006
发表于 2014-2-11 17:11:49
lanjqka 发表于 2014-2-11 17:01 static/image/common/back.gif
就是这个了。。。谢谢。。。脑子笨,感觉还是很难。。。。