Initial commit
This commit is contained in:
commit
c23db16aac
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@ -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
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.idea/
|
19
go-speed.go
Normal file
19
go-speed.go
Normal file
@ -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
|
||||
}
|
44
go-speed_test.go
Normal file
44
go-speed_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
14
go.mod
Normal file
14
go.mod
Normal file
@ -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
|
||||
)
|
12
go.sum
Normal file
12
go.sum
Normal file
@ -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=
|
34
lua-speed.go
Normal file
34
lua-speed.go
Normal file
@ -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()))
|
||||
}
|
20
lua-speed_test.go
Normal file
20
lua-speed_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user