Lua performance in different environments

note, Apr 11, 2025, on Mitja Felicijan's blog

A quick look into difference in performance when running Lua normally vs Luajit vs Lua embedded in C program.

You can also check code on GitHub @mitjafelicijan/probe/c-luajit.

Do not take this data as a benchmark, it's just a naive quick checkup.

Naive and intentionally slow implementation of Fibonacci sequence

-- Naive recursive implementation to increase the time of computation.
function fibonacci(n)
    if n == 0 then
        return 0
    elseif n == 1 then
        return 1
    else
        return fibonacci(n - 1) + fibonacci(n - 2)
    end
end

local n = 40
local result = fibonacci(n)
print("The " .. n .. "th Fibonacci number is: " .. result)

Embedded Lua primer

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

int main(void) {
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    if (luaL_loadfile(L, "fibonacci.luac") || lua_pcall(L, 0, 0, 0)) {
        fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
        return 1;
    }

    lua_close(L);
    return 0;
}

Quick results

LuaLuajitLua embedded in C
6.006s1.065s1.065s

Test script

#!/usr/bin/env bash

ITERATIONS=120

# Just Lua interpreter
for i in $(seq 1 $ITERATIONS); do
    echo "> lua run #$i/$ITERATIONS"
    /usr/bin/time -f "%e,%U,%S" lua fibonacci.lua > /dev/null 2>> out.lua.txt
done

# Using Luajit
for i in $(seq 1 $ITERATIONS); do
    echo "> luajit run #$i/$ITERATIONS"
    /usr/bin/time -f "%e,%U,%S" luajit fibonacci.lua > /dev/null 2>> out.luajit.txt
done

# With C and Luajit
for i in $(seq 1 $ITERATIONS); do
    echo "> cluajit run #$i/$ITERATIONS"
    /usr/bin/time -f "%e,%U,%S" ./fibonacci > /dev/null 2>> out.cluajit.txt
done

Other notes

DateTitle
Install Plan9port on Linuxplan9
Mount Plan9 over networkplan9
10/GUI 10 Finger Multitouch User Interface
Simple presentations with Markdown
Extend Lua with custom C functions using Clangc
Alacritty open links with modifier
Uninstall Ollama from a Linux box
Display xterm color palette
Make DCSS playable on 4k displaysdcss
Personal sane Vim defaultsvim
Aerial photography of algae spotted on river Sava
Take a screenshot in Plan9plan9
Run 9front in Qemuplan9
Grep to Less that maintain colors
Change Alt key to Win key under Xfce4