diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-04-11 22:03:18 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-04-11 22:03:18 +0200 |
| commit | e075c846fff22c677a06cc94bd2326c1cf615714 (patch) | |
| tree | 29941da94a490260922cbe65ece3f3e4301dac70 /content/notes | |
| parent | 03dec3f9ade59cad2951ca9dd682df6afb12388d (diff) | |
| download | mitjafelicijan.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.md | 88 |
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 | --- | ||
| 2 | title: Lua performance in different environments | ||
| 3 | url: lua-performance-in-different-environments.html | ||
| 4 | date: 2025-04-11T16:13:13+02:00 | ||
| 5 | type: note | ||
| 6 | draft: false | ||
| 7 | tags: [] | ||
| 8 | --- | ||
| 9 | |||
| 10 | A quick look into difference in performance when running Lua normally vs Luajit | ||
| 11 | vs Lua embedded in C program. | ||
| 12 | |||
| 13 | You can also check code on GitHub | ||
| 14 | [@mitjafelicijan/probe/c-luajit](https://github.com/mitjafelicijan/probe/tree/master/c-luajit). | ||
| 15 | |||
| 16 | Do 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. | ||
| 22 | function 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 | ||
| 30 | end | ||
| 31 | |||
| 32 | local n = 40 | ||
| 33 | local result = fibonacci(n) | ||
| 34 | print("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 | |||
| 44 | int 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 | |||
| 69 | ITERATIONS=120 | ||
| 70 | |||
| 71 | # Just Lua interpreter | ||
| 72 | for 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 | ||
| 75 | done | ||
| 76 | |||
| 77 | # Using Luajit | ||
| 78 | for 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 | ||
| 81 | done | ||
| 82 | |||
| 83 | # With C and Luajit | ||
| 84 | for 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 | ||
| 87 | done | ||
| 88 | ``` | ||
