本帖最后由 dcl1214 于 2025-3-4 13:34 编辑
字符串拼接,调用频繁且字符量大的时候,速度很关键,哪位还有更快的字符串拼接,一起切磋一下
 - (defun $pin-jie$
- (strs fgf / a bars len str str str-last strs-car strs-n)
- (if (and strs
- fgf
- (= (type strs) 'list) ;必须是表格式
- (setq strs (vl-remove nil strs)) ;空对象删除
- (setq strs-car (CAR strs))
- (= (type strs-car) 'STR)
- ;第一个必须是字符串
- (= (type fgf) 'str)
- )
- (progn
- (setq strs (cdr strs))
- (setq strs (reverse strs))
- (setq strs-n nil)
- (while (setq a (car strs))
- (setq strs-n (cons a strs-n))
- (setq strs-n (cons fgf strs-n))
- (setq strs (cdr strs))
- )
- (setq strs-n (cons strs-car strs-n))
- ;添加分隔符
- (setq str (vl-catch-all-apply 'apply (list 'strcat strs-n)))
- ;快速拼接一次
- (if (vl-catch-all-error-p str) ;拼接出错了(估计出现了非字串现象)
- (progn
- (setq
- strs-n (mapcar
- (function (lambda (a)
- (if (and a (/= (type a) 'str))
- (setq a (vl-prin1-to-string a))
- )
- a
- )
- )
- strs-n
- )
- ) ;强制转换为字串格式
- (setq str (vl-catch-all-apply 'apply (list 'strcat strs-n)))
- ;再次拼接
- (if (vl-catch-all-error-p str) ;拼接出错了
- (setq str nil) ;做空变量
- )
- )
- )
- )
- )
- str ;返回给上一级
- )
- 共计:900000个字符,耗时:0.985
- ; 9 表格 从 #<editor "C:/Users/Administrator/Desktop/字符拼接.LSP"> 加载
- _$
- 共计:900000个字符,耗时:0.937
- ; 9 表格 从 #<editor "C:/Users/Administrator/Desktop/字符拼接.LSP"> 加载
- _$
- 共计:900000个字符,耗时:0.953
- ; 9 表格 从 #<editor "C:/Users/Administrator/Desktop/字符拼接.LSP"> 加载
- _$
- 共计:1000000个字符,耗时:1.375
- ; 9 表格 从 #<editor "C:/Users/Administrator/Desktop/字符拼接.LSP"> 加载
- _$
- 共计:1000000个字符,耗时:1.062
|