首页 > 解决方案 > 为什么相同的代码在 lua 中比在 julia 中慢?

问题描述

当我写出相同的代码来找到一个数字的最小倍数时,Julia 比 Lua 快得多,分别需要 ~3 秒和 ~23 秒。

这是我的 Julia 代码:

function smallest_multiple(x)
    n = 1
    while true
        for i = 1:x
            if n % i != 0
                break
            elseif i == x
                return n
            end
        end
        n += 1
    end
end

println(smallest_multiple(20))

这是 Lua 代码:

function Smallest_Multiple(x)
    local n = 1
    while true do
        for i = 1,x do
            if n % i ~= 0 then
                break
            elseif i == x then
                return n
            end
        end
        n = n + 1
    end
end

语言之间是否存在根本的低级差异或代码中的疏忽?

标签: luajulia

解决方案


推荐阅读