Python with Go

I don't know what number to brew, but I use a package written in Golang from Python (multilingual)

1. Configuration

Python : 2.7 Golang : 1.7 OS : CentOS release 6.8 Memory : 2048MB CPU : 3core

PythonLibrary ctypes

2. Preparation

fib.go


package main

import "C"

//export fib
func fib(n uint) uint {
    if n <= 1 {
        return n
    }
    return fib(n-1) + fib(n-2)
}

func main() {}

build

go build -o fib.so  -buildmode=c-shared fib.go

fib_go.py


from ctypes import *

lib = cdll.LoadLibrary("./fib.so")

## arg
lib.fib.argtypes = [c_longlong]
## return
lib.fib.restype = c_longlong
print "fib  %d" % lib.fib(40)

fib.py


#!/usr/bin/env python

def fib(n):
    if n <= 1:
        return n
    else:
        return fib(n-1) + fib(n-2)

print(fib(40))

3. Measurement

fib_go.py

#time python fib_go.py
fib  102334155

real	0m1.171s
user	0m1.105s
sys	0m0.071s

fib.py

#time python fib_go.py
102334155

real	0m46.598s
user	0m46.449s
sys	0m0.030s

Recommended Posts

Python with Go
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Python starting with Windows 7
Heatmap with Python + matplotlib
Multi-process asynchronously with python
Python programming with Atom