Go language to see and remember Part 8 Call GO language from Python

This time, it is an approach to speed up, which is a weak point of python, by calling GO language from Python.

When we talk about Python, it's not a "compiler." It went up in flames.

Neither java nor "compiler" nor C # is a compiler.

Converting to an intermediate language and executing it is slow and the first battery runs out quickly.

** 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

10 times faster.

Recommended Posts

Go language to see and remember Part 8 Call GO language from Python
C language to see and remember Part 2 Call C language from Python (argument) string
C language to see and remember Part 1 Call C language from Python (hello world)
C language to see and remember Part 4 Call C language from Python (argument) double
C language to see and remember Part 5 Call C language from Python (argument) Array
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
Go language to see and remember Part 7 C language in GO language
Call Matlab from Python to optimize
Call C language functions from Python to exchange multidimensional arrays
Python to switch from another language
Call c language from python (python.h)
[It's not too late to learn Python from 2020] Part 3 Python Language Basic (1)
Porting and modifying doublet-solver from python2 to python3.
[Python] How to read data from CIFAR-10 and CIFAR-100
List of Python code to move and remember
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➀
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➁
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[Python] How to call a c function from python (ctypes)
The story of porting code from C to Go and getting hooked (and to the language spec)
Call CPLEX from Python (DO cplex)
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
Post from Python to Slack
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➀
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
To go back and forth between standard python, numpy, pandas ①
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➁
Connect to sqlite from python
[Go language] Use OpenWeatherMap and Twitter API to regularly tweet weather information from Raspberry Pi
How to call Python or Julia from Ruby (experimental implementation)
Ported from R language of "Sazae-san's rock-paper-scissors data analysis" to Python
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
[First Ripple] [I am] Call JavaScript (Node.js) from Python and process Ripple
[Python] Hit Keras from TensorFlow and TensorFlow from c ++ to speed up execution
Install mecab on Sakura shared server and call it from python
How to get followers and followers from python using the Mastodon API
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Simple code to call a python program from Javascript on EC2
Call dlm from python to run a time-varying coefficient regression model
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
How to use Python with Jw_cad (Part 2 Command explanation and operation)
Call your own C language shared library from Python using ctypes
[Python] Try to recognize characters from images with OpenCV and pyocr
Call a Python function from p5.js.
Call C from Python with DragonFFI
Python 3.6 on Windows ... and to Xamarin.
perl objects and python class part 2.
[Introduction to Python3 Day 1] Programming and Python
Python, yield, return, and sometimes yield from
Create folders from '01' to '12' with python
Introduction to Python Hands On Part 1
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Call popcount from Ruby / Python / C #
Read and use Python files from Python
Introduction to Protobuf-c (C language ⇔ Python)