An experiment to run Go code directly from Python.
libfib.go
is a very naive implementation of Fibonacci sequence. Using Go's build modes, we build it into a shared library. The library is imported in Python and used there.
Basically, we execute the Go code from Python code even though the latter doesn't even know what Go is.
Run go help buildmodes
to learn more about Go's build modes.
- It's cool.
- Go is much faster than Python. You can rewrite slow parts of your code in Go, and call them from your Python code. See benchmarks.
- It's cool.
Benchmarks are the best way to compare the performance and really show the coolness of Go's build modes.
The function to benchmark: Find the first 40 Fibonacci numbers. That is Fib
function will be called with n
from 1...40
. The execution time of the full loop is then measured.
Results: libfib
is 76X faster!!!1111
benchmark.py
implements the same Fibonacci function in pure Python.Here's the result of the Linux time
command:
$ time python benchmark.py
real 2m26.726s
user 2m6.232s
sys 0m2.280s
benchmark_libfib.py
calls the Fibonacci function in the shared library from Python. Here's the result of the Linux time
command:
$ time python benchmark_libfib.py
real 0m1.899s
user 0m1.593s
sys 0m0.039s
go build -buildmode=c-shared -o libfib.so libfib.go
This will produce fib.h
and fib.so
.
Now we can load the library in Python and start using it.
import ctypes
lib = ctypes.CDLL("libfib.so")
print(lib.Fib(20))
MIT (c) Karan Goel