You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Julia性能落后Node/Bun20倍?求代码优化与性能差异解析

Julia与Bun/Node的性能差异疑问

我编写了一段JS代码,用于生成满足特定属性的数字列表:

// Config //
const maxNum = 30; //The largest number that can be used (even)
const maxLength = maxNum; // The max length of a group

// Default vars
const res = [];
const even = [];
const odd = [];
for(let i = 1; i < maxNum + 1; i++) 
    i%2==0 ? even.push(i) : odd.push(i);

const addNumber = (curr, max, e) => {
    if(curr.length == max) return res.push(curr);
    for(let num of e ? even : odd) {
        if(curr.includes(num) || curr[curr.length-1] > num) continue;
        addNumber([...curr, num], max, !e);
    }
}

for(let i = 1; i < maxLength + 1; i++)
    addNumber([], i, false);

因Julia以数学运算性能优异著称,我将其转写为Julia代码:

const MaxNumber = 10; # Needs to be even!

const res = Vector{Int16}[];
const even = [n*2 for n in 1:(convert(Int16, MaxNumber/2))];
const odd = [n*2-1 for n in 1:(convert(Int16, MaxNumber/2))];

function addNumber(curr::Vector{Int16}, max::Number, toDoEven::Bool)
    if (length(curr) === max)
        return push!(res, curr);
    end
    for num::Int16 in (toDoEven ? even : odd)
        if(!isempty(curr) && (num in curr || last(curr) > num))
            continue;
        end
        addNumber(vcat(curr, num), max, !toDoEven);
    end
end

for i in 1:MaxNumber
    addNumber(Int16[], i, false)
end

(测试时使用Int8替代Int16,修改后支持maxNum=30)

通过Hyperfine基准测试发现,Julia执行耗时为368-395ms,而Bun(Node紧随其后)仅需13-125ms,性能差距达20倍。热身测试及将maxNum提升至30后,Julia仍大幅落后。想请教是代码未优化、操作失误,还是编译型的Node/Bun确实优于JIT的Julia?

内容的提问来源于stack exchange,提问作者BelMat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 06:51:17