commit c23db16aac06c8a48014701095ce8aaa9864a1b3 Author: MrMelon54 Date: Wed Nov 13 20:00:48 2024 +0000 Initial commit diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e497157 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://editorconfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +charset = utf-8 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/go-speed.go b/go-speed.go new file mode 100644 index 0000000..ea363b5 --- /dev/null +++ b/go-speed.go @@ -0,0 +1,19 @@ +package lua_speed + +func Fibonacci(total uint64) uint64 { + a := uint64(1) + b := uint64(1) + for i := range total { + switch i { + case 0, 1: + return i + case 2: + return 1 + default: + c := a + b + a = b + b = c + } + } + return b +} diff --git a/go-speed_test.go b/go-speed_test.go new file mode 100644 index 0000000..0af8937 --- /dev/null +++ b/go-speed_test.go @@ -0,0 +1,44 @@ +package lua_speed + +import ( + "github.com/negrel/assert" + "testing" +) + +var fibonacciNumbers = [][2]uint64{ + {0, 0}, + {1, 1}, + {1, 2}, + {2, 3}, + {3, 4}, + {5, 5}, + {8, 6}, + {13, 7}, + {21, 8}, + {34, 9}, + {55, 10}, + {89, 11}, + {144, 12}, + {23, 13}, + {377, 14}, + {610, 15}, + {987, 16}, + {1597, 17}, + {2584, 18}, + {4181, 19}, + {6765, 20}, + {12_586_269_025, 50}, + {99_194_853_094_755_497, 83}, +} + +func TestFibonacci(t *testing.T) { + for _, i := range fibonacciNumbers { + assert.Equal(i[0], Fibonacci(i[1])) + } +} + +func BenchmarkFibonacci(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Fibonacci(83) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..aee0c2a --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module test-lua-speed + +go 1.23.3 + +require ( + github.com/negrel/assert v0.3.0 + github.com/yuin/gopher-lua v1.1.1 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..32ba0ad --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/negrel/assert v0.3.0 h1:B1rfuvBBJuhiD0B7mck8NkBQuFEwYghS8nkBB3Z/wGY= +github.com/negrel/assert v0.3.0/go.mod h1:uMt1lWEMiyJuq4jkSkx7KhpJQjTlJKx2DgU6cQ5v4lU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= +github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/lua-speed.go b/lua-speed.go new file mode 100644 index 0000000..3292d30 --- /dev/null +++ b/lua-speed.go @@ -0,0 +1,34 @@ +package lua_speed + +import ( + _ "embed" + "github.com/yuin/gopher-lua" + "log" +) + +//go:embed speed.lua +var speedLuaScript string + +func FibonacciLuaInit() *lua.LState { + L := lua.NewState() + L.OpenLibs() + + if err := L.DoString(speedLuaScript); err != nil { + log.Panic("Error running Lua plugin:", err) + } + + return L +} + +func FibonacciLuaRun(L *lua.LState, n uint64) uint64 { + F := L.GetGlobal("Fibonacci") + err := L.CallByParam(lua.P{ + Fn: F, + NRet: 1, + Protect: true, + }, lua.LNumber(n)) + if err != nil { + return 0 + } + return uint64(L.ToNumber(L.GetTop())) +} diff --git a/lua-speed_test.go b/lua-speed_test.go new file mode 100644 index 0000000..b481807 --- /dev/null +++ b/lua-speed_test.go @@ -0,0 +1,20 @@ +package lua_speed + +import ( + "github.com/negrel/assert" + "testing" +) + +func TestFibonacciLua(t *testing.T) { + L := FibonacciLuaInit() + for _, i := range fibonacciNumbers { + assert.Equal(i[0], FibonacciLuaRun(L, i[1])) + } +} + +func BenchmarkFibonacciLuaRun(b *testing.B) { + L := FibonacciLuaInit() + for i := 0; i < b.N; i++ { + _ = FibonacciLuaRun(L, 83) + } +} diff --git a/speed.lua b/speed.lua new file mode 100644 index 0000000..4fc56fb --- /dev/null +++ b/speed.lua @@ -0,0 +1,16 @@ +function Fibonacci(total) + a = 1 + b = 1 + for n = 0, total do + if n == 0 then + return 0 + elseif n == 1 or n == 2 then + return 1 + else + c = a + b + a = b + b = c + end + end + return b +end