by bilinguliar · 2y ago · 0 comments
func isOdd(n int) bool {
var odd bool
for i := 0; i < n; i++ {
odd = !odd
}
return odd
}
Playground link: https://go.dev/play/p/8TIfzGrdWDFI did not profile this one yet. But my intuition, and my industry experience, tell me that this is fast.
func isOdd(n int) bool {
switch {
case n == 0:
return false
case n > 0:
return !isOdd(n-1)
default:
return !isOdd(n+1)
}
} let rec isEven n =
if n = 0 then true
else isOdd (n-1)
and isOdd n =
not (isEven n)You may need to call setrlimit with an appropriate RLIMIT_STACK, but I think the FFI compatibility should be able to pull that off, right?
Doesn't Go allocate / extend stacks using dynamic allocation rather than having a static limit (e.g. like C)?
playground::isOdd:
testq %rdi, %rdi
setg %al
andb %dil, %al
retq
(Click ... beside build to get assembly) https://play.rust-lang.org/?version=stable&mode=release&edit...Unfortunately the go playground doesn't seem to support emitting assembly?
No, Go playground can not do that :(
https://godbolt.org/z/eMv41nc6Y
Proof, that you must use rust if you want blazingly fast execution of fearlessly pessimized code!
func isEven(n int64) bool {
return !isOdd(n)
}