[ Skill ] append1, append, nconc, tconc, lconc, cons 效率对比

https://www.cnblogs.com/yeungchie/

先说结论: cons > tconc, lconc >> nconc > append1, append

append1

lisp 复制代码
let((a)
    ycTime(
        for(i 1 fix(3e4)
            a = append1(a i)
        )
    )
    length(a)
)
; UserTime  : 12.108453s
; SysTime   : 0.000000s
; WallClock : 12.104178s
; 30000

append

lisp 复制代码
let((a)
    ycTime(
        for(i 1 fix(3e4)
            a = append(a list(i))
        )
    )
    length(a)
)
; UserTime  : 13.654966s
; SysTime   : 0.000000s
; WallClock : 13.651223s
; 30000

append1, append 这两个函数操作写链表速度奇慢,因为它每次追加元素都需要把整个链表遍历一次。

唯一的好处就是对初学者友好,容易理解和使用,因此在知道数据量不大的前提下,才去使用这两个函数吧。

nconc

lisp 复制代码
let((a)
    ycTime(
        for(i 1 fix(3e4)
            a = nconc(a list(i))
        )
    )
    length(a)
)
; UserTime  : 2.995670s
; SysTime   : 0.000000s
; WallClock : 2.994434s
; 30000

相比 append 快了一点,但也没有快很多,毕竟只追加 30000 次。

它对于输入变量是具有破坏性的,会直接修改变量,而不像 append 在底层会将数据进行拷贝,因此会快一些,更省内存。

tconc

lisp 复制代码
let((a)
    ycTime(
        for(i 1 fix(3e4)
            a = tconc(a i)
        )
        a = car(a)
    )
    length(a)
)
; UserTime  : 0.001871s
; SysTime   : 0.000000s
; WallClock : 0.001871s
; 30000

tconc 会保留链表中最后一个节点,当追加列表时直接操作该节点,因此不需要反复遍历整个链表。car 指向我们期望的链表。

在上面的情况下,相比 append 快了近 5000 倍,已经没有可比性了,所以后面函数把追加次数调整为 60000000 来比较。

lisp 复制代码
let((a)
    ycTime(
        for(i 1 fix(6e7)
            a = tconc(a i)
        )
        a = car(a)
    )
    length(a)
)
; UserTime  : 4.085272s
; SysTime   : 0.000189s
; WallClock : 4.083197s
; 60000000

lconc

lisp 复制代码
let((a)
    ycTime(
        for(i 1 fix(6e7)
            a = lconc(a list(i))
        )
        a = car(a)
    )
    length(a)
)
; UserTime  : 4.741206s
; SysTime   : 0.000000s
; WallClock : 4.740194s
; 60000000

两者区别在于,tconc 拼接的是一个标量,lconc 拼接的是一个链表。

速度相差不大,看情况来使用吧。

cons

lisp 复制代码
let((a)
    ycTime(
        for(i 1 fix(6e7)
            a = cons(i a)
        )
    )
    length(a)
)
; UserTime  : 1.865164s
; SysTime   : 0.000000s
; WallClock : 1.865343s
; 60000000

这个函数又快不少,它是将元素追加到链表开头,因此不需要遍历元素,也不需要记忆节点。

由于是向前追加,元素顺序可能会跟我们期望的相反,这时候就需要降链表翻转一下,看看速度。

lisp 复制代码
let((a)
    ycTime(
        for(i 1 fix(6e7)
            a = cons(i a)
        )
        a = reverse(a)
    )
    length(a)
)
; UserTime  : 3.655040s
; SysTime   : 0.000000s
; WallClock : 3.649090s
; 60000000

翻转增加了一点耗时,速度跟 tconclconc 差不多。

相关推荐
kft131412 小时前
代码转需求文档skill_浅木·先生
需求分析·skill·skills
煎饼学大模型20 小时前
架构决定上限:Skill 知识架构的三次重构实践
java·重构·架构·skill
MicrosoftReactor1 天前
技术速递|智能体测试智能体:基于 Foundry Hosted Agents 构建云原生 Skill-Eval Harness
ai·云原生·agent·ai-agent·skill
菩提小狗1 天前
AI每日资讯|AI落地|最新情报|skill精选|2026年07月21日(11案例+10爆款Skill)
大模型·agent·skill·ai资讯·ai落地
AI砖家2 天前
多智能体系统实战:架构设计、数据库表设计与 Skill 体系
数据库·多智能体·skill·agent架构设计·agengt
赵康2 天前
AI 写代码之后,Code Review 会议怎么开
ai·llm·skill
大强同学4 天前
Kimi Code CLI
人工智能·agent·claude·skill·kimi code cli
wMoqi6 天前
trae cn 中ai开发配置skill mcp 规则参考(yudao-cloud项目-个人用)
ai·skill·yudao·mcp·trae
KaiwuDB6 天前
kwdb-performance-review Skill:四步闭环搞定数据库慢 SQL
数据库·sql·agent·kaiwudb·skill
嘛也学不会9 天前
OpenSpec + Superpowers 的一个使用思路
教程·基础·skill·openspec·superpower