** GO is a real compiler. ** **
** From the perspective of python, it may be possible to speed up the calculation part. ** **
I referred to Fibonacci number.
――One pair of rabbits gives birth to one rabbit every month from two months after birth. -Rabbits never die. --Under these conditions, how many newborn rabbits will become in a year?
fib_go.py
from ctypes import *
lib = cdll.LoadLibrary("./fib.so")
for i in range(40):
print ("fib %d %d" %(i, lib.fib(i)))
Just call the following function in lib.fib (i).
fib.go
package main
import "C"
//export fib
func fib(n int) int {
if n <= 1 { return n }
return fib(n-1) + fib(n-2)
}
func main() {}
** Compile a function written in Go. ** **
> go build -o fib.so -buildmode=c-shared fib.go
Execute
>python fib_go.py
fib 0 0
fib 1 1
fib 2 1
fib 3 2
fib 4 3
fib 5 5
fib 6 8
fib 7 13
fib 8 21
fib 9 34
fib 10 55
fib 11 89
fib 12 144
fib 13 233
fib 14 377
fib 15 610
fib 16 987
fib 17 1597
fib 18 2584
fib 19 4181
fib 20 6765
fib 21 10946
fib 22 17711
fib 23 28657
fib 24 46368
fib 25 75025
fib 26 121393
fib 27 196418
fib 28 317811
fib 29 514229
fib 30 832040
fib 31 1346269
fib 32 2178309
fib 33 3524578
fib 34 5702887
fib 35 9227465
fib 36 14930352
fib 37 24157817
fib 38 39088169
fib 39 63245986
fib 40 102334155
Recommended Posts