Home > Enterprise >  Love Lua FFI is faster with medium sized functions, but slower with small and large functions
Love Lua FFI is faster with medium sized functions, but slower with small and large functions

Time:06-03

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

CodePudding user response:

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.

CodePudding user response:

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

-- main.lua
-- 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

  • Related