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
Make B/W SVG charts with matplotlib
Currated list of Vim ALE linters
60's IBM Computers Commercial
Using ffmpeg to combine videos side by side
Making cgit look nicer
Download list of YouTube files
Development environments with Nix
Online radio streaming with MPV from terminal
Add lazy loading of images in Jekyll posts
Change Alt key to Win key under Xfce4
Mount Plan9 over networkplan9
Previews how man page written in Troff will look like
Execute not blocking async shell command in C#
Embedding resources into binary with Cc
Set color temperature of displays on i3