Love Lua ffi具有中等大小的功能更快,但大小功能较慢

发布于 2025-02-03 19:52:07 字数 1859 浏览 4 评论 0 原文

我一直在使用Love 2D了解Luajit和Lua FFI库。为了测试FFI是否真的更快,我编码了一个函数以计数一个范围内的所有素数。

(代码可能不准确,我只是想要两种语言都可以得到相同答案的硬数学问题)

// test.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double numberBuffer;

double getPrimes(double n) {
    double count = 0;

    for (double i = 1; i <= n; i++) {
        double cap = pow(i, 0.5);

        for (double num = 2; num <= cap; num++) {
            if (fmod(i, num) == 0) {
                count++;
                break;
            }
        }
    }

    numberBuffer = count;
    return numberBuffer;
}
-- main.lua
local ffi = require("ffi")

function loadFFI(name)
    local dir = love.filesystem.getRealDirectory("bin/" .. name .. ".so")
    return ffi.load(dir .. "bin/" .. name .. ".so")
end

local test = loadFFI("test")

ffi.cdef[[
double getPrimes(double n);
]]

local function getPrimes(n)
    local count = 0

    for i = 1,n do
        for num = 2, i^(0.5) do
            if (i % num) == 0 then
                count = (count + 1)
                break
            end
        end
    end

    return count
end

function love.load()
    local one, two = 0, 0
    local n = 60000

    local time = love.timer.getTime()

    local c = test.getPrimes(n)

    one = (love.timer.getTime() - time)
    time = love.timer.getTime()

    local lua = getPrimes(n)

    two = (love.timer.getTime() - time)

    print("n = " .. tostring(n))
    print("C", c, (tostring(one * 1000) .. " miliseconds"))
    print("Lua", lua, (tostring(two * 1000) .. " miliseconds"))
end

起初,结果是我预期的。我认为,卢阿(Lua)的速度很小,因为开销。使用稍大的集合,C变得更快。

但是,由于非常大的集合,C变慢得多。为什么? LUA

I've been learning about LuaJIT and the Lua FFI library using Love 2D. To test if FFI was really faster, I coded a function to count all prime numbers in a range.

(Code probably isn't accurate, I just wanted a hard math problem that both languages could get the same answer on)

// test.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double numberBuffer;

double getPrimes(double n) {
    double count = 0;

    for (double i = 1; i <= n; i++) {
        double cap = pow(i, 0.5);

        for (double num = 2; num <= cap; num++) {
            if (fmod(i, num) == 0) {
                count++;
                break;
            }
        }
    }

    numberBuffer = count;
    return numberBuffer;
}
-- main.lua
local ffi = require("ffi")

function loadFFI(name)
    local dir = love.filesystem.getRealDirectory("bin/" .. name .. ".so")
    return ffi.load(dir .. "bin/" .. name .. ".so")
end

local test = loadFFI("test")

ffi.cdef[[
double getPrimes(double n);
]]

local function getPrimes(n)
    local count = 0

    for i = 1,n do
        for num = 2, i^(0.5) do
            if (i % num) == 0 then
                count = (count + 1)
                break
            end
        end
    end

    return count
end

function love.load()
    local one, two = 0, 0
    local n = 60000

    local time = love.timer.getTime()

    local c = test.getPrimes(n)

    one = (love.timer.getTime() - time)
    time = love.timer.getTime()

    local lua = getPrimes(n)

    two = (love.timer.getTime() - time)

    print("n = " .. tostring(n))
    print("C", c, (tostring(one * 1000) .. " miliseconds"))
    print("Lua", lua, (tostring(two * 1000) .. " miliseconds"))
end

At first the results were as I expected. With small sets, Lua is faster, I assume because of overhead. With slightly larger sets, C becomes much faster.

With very large sets however, C becomes much slower. Why?
Results from Lua FFI prime numbers speed test indicating C gets faster, then slower

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2025-02-10 19:52:07

感谢Ikegami在评论中回答。
将双打更改为INT,并用%操作员替换FMOD解决了问题。

Thanks to Ikegami for answering in comments.
Changing the doubles to ints, and replacing fmod with the % operator solved the issue.

Image showing updated C code with faster C results.

断念 2025-02-10 19:52:07

我想知道您不使用 love.graphics.print() in love.draw() ...

-- main.lua

local ffi = require("ffi")

local test = ffi.load("./test.so")

ffi.cdef[[
int getPrimes(int n);
]]

local function getPrimes(n)
    local count = 0

    for i = 1,n do
        for num = 2, i^(0.5) do
            if (i % num) == 0 then
                count = (count + 1)
                break
            end
        end
    end

    return count
end

local one, two, n, time, c, lua = 0, 0, 60000, 0, 0, 0                   

function love.update()
    time = love.timer.getTime()
    c = test.getPrimes(n)
    one = (love.timer.getTime() - time)
    time = love.timer.getTime()
    lua = getPrimes(n)
    two = (love.timer.getTime() - time)
end

function love.draw()
    love.graphics.print("n\t" .. tostring(n), 100, 100)
    love.graphics.print("C\t" .. c .. "\t" .. (tostring(one * 1000) .. " miliseconds"), 100, 200)
    love.graphics.print("Lua\t" .. lua .. "\t" .. (tostring(two * 1000) .. " miliseconds"), 100, 300)
end

I am wondering about that you not using love.graphics.print() in love.draw()...

-- main.lua

local ffi = require("ffi")

local test = ffi.load("./test.so")

ffi.cdef[[
int getPrimes(int n);
]]

local function getPrimes(n)
    local count = 0

    for i = 1,n do
        for num = 2, i^(0.5) do
            if (i % num) == 0 then
                count = (count + 1)
                break
            end
        end
    end

    return count
end

local one, two, n, time, c, lua = 0, 0, 60000, 0, 0, 0                   

function love.update()
    time = love.timer.getTime()
    c = test.getPrimes(n)
    one = (love.timer.getTime() - time)
    time = love.timer.getTime()
    lua = getPrimes(n)
    two = (love.timer.getTime() - time)
end

function love.draw()
    love.graphics.print("n\t" .. tostring(n), 100, 100)
    love.graphics.print("C\t" .. c .. "\t" .. (tostring(one * 1000) .. " miliseconds"), 100, 200)
    love.graphics.print("Lua\t" .. lua .. "\t" .. (tostring(two * 1000) .. " miliseconds"), 100, 300)
end

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文