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
Sane default for Dungeon Crawl Stone Soup Online editiondcss
60's IBM Computers Commercial
Write and read structs to/from files in Cc
Execute not blocking async shell command in C#
#cat-v on weechat configuration
Edsger W. Dijkstra Manuscripts ebook
Mount Plan9 over networkplan9
Write ISO to USB Key
Install Plan9port on Linuxplan9
Simple presentations with Markdown
Embedding resources into binary with Cc
Compile drawterm on Fedora 38plan9
Extract lines from a file with sed
Cronjobs on Github with Github Actions
Set color temperature of displays on i3