aboutsummaryrefslogtreecommitdiff
path: root/content/notes
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2025-04-11 22:03:18 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2025-04-11 22:03:18 +0200
commite075c846fff22c677a06cc94bd2326c1cf615714 (patch)
tree29941da94a490260922cbe65ece3f3e4301dac70 /content/notes
parent03dec3f9ade59cad2951ca9dd682df6afb12388d (diff)
downloadmitjafelicijan.com-e075c846fff22c677a06cc94bd2326c1cf615714.tar.gz
New note: Lua performance
Diffstat (limited to 'content/notes')
-rw-r--r--content/notes/2025-04-11-lua-performance-in-different-environments.md88
1 files changed, 88 insertions, 0 deletions
diff --git a/content/notes/2025-04-11-lua-performance-in-different-environments.md b/content/notes/2025-04-11-lua-performance-in-different-environments.md
new file mode 100644
index 0000000..e1f67ee
--- /dev/null
+++ b/content/notes/2025-04-11-lua-performance-in-different-environments.md
@@ -0,0 +1,88 @@
1---
2title: Lua performance in different environments
3url: lua-performance-in-different-environments.html
4date: 2025-04-11T16:13:13+02:00
5type: note
6draft: false
7tags: []
8---
9
10A quick look into difference in performance when running Lua normally vs Luajit
11vs Lua embedded in C program.
12
13You can also check code on GitHub
14[@mitjafelicijan/probe/c-luajit](https://github.com/mitjafelicijan/probe/tree/master/c-luajit).
15
16Do not take this data as a benchmark, it's just a naive quick checkup.
17
18## Naive and intentionally slow implementation of Fibonacci sequence
19
20```lua
21-- Naive recursive implementation to increase the time of computation.
22function fibonacci(n)
23 if n == 0 then
24 return 0
25 elseif n == 1 then
26 return 1
27 else
28 return fibonacci(n - 1) + fibonacci(n - 2)
29 end
30end
31
32local n = 40
33local result = fibonacci(n)
34print("The " .. n .. "th Fibonacci number is: " .. result)
35```
36
37## Embedded Lua primer
38
39```sh
40#include <lua.h>
41#include <lualib.h>
42#include <lauxlib.h>
43
44int main(void) {
45 lua_State *L = luaL_newstate();
46 luaL_openlibs(L);
47
48 if (luaL_loadfile(L, "fibonacci.luac") || lua_pcall(L, 0, 0, 0)) {
49 fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
50 return 1;
51 }
52
53 lua_close(L);
54 return 0;
55}
56```
57
58## Quick results
59
60| Lua | Luajit | Lua embedded in C |
61|--------|--------|-------------------|
62| 6.006s | 1.065s | 1.065s |
63
64## Test script
65
66```sh
67#!/usr/bin/env bash
68
69ITERATIONS=120
70
71# Just Lua interpreter
72for i in $(seq 1 $ITERATIONS); do
73 echo "> lua run #$i/$ITERATIONS"
74 /usr/bin/time -f "%e,%U,%S" lua fibonacci.lua > /dev/null 2>> out.lua.txt
75done
76
77# Using Luajit
78for i in $(seq 1 $ITERATIONS); do
79 echo "> luajit run #$i/$ITERATIONS"
80 /usr/bin/time -f "%e,%U,%S" luajit fibonacci.lua > /dev/null 2>> out.luajit.txt
81done
82
83# With C and Luajit
84for i in $(seq 1 $ITERATIONS); do
85 echo "> cluajit run #$i/$ITERATIONS"
86 /usr/bin/time -f "%e,%U,%S" ./fibonacci > /dev/null 2>> out.cluajit.txt
87done
88```