Python avec Go

Je ne sais pas quel numéro brasser, mais j'utilise un package écrit en Golang depuis Python (multilingue)

1. Configuration

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

PythonLibrary ctypes

2. Préparation

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. Mesure

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 avec Go
FizzBuzz en Python3
Grattage avec Python
Statistiques avec python
Grattage avec Python
Twilio avec Python
Intégrer avec Python
Jouez avec 2016-Python
AES256 avec python
Testé avec Python
python commence par ()
avec syntaxe (Python)
Bingo avec python
Zundokokiyoshi avec python
Excel avec Python
Micro-ordinateur avec Python
Cast avec python
Communication série avec Python
Django 1.11 a démarré avec Python3.6
Jugement des nombres premiers avec Python
Python avec eclipse + PyDev.
Communication de socket avec Python
Analyse de données avec python 2
Grattage en Python (préparation)
Essayez de gratter avec Python.
Apprendre Python avec ChemTHEATER 03
"Orienté objet" appris avec python
Exécutez Python avec VBA
Manipuler yaml avec python
Résolvez AtCoder 167 avec python
Communication série avec python
[Python] Utiliser JSON avec Python
Apprendre Python avec ChemTHEATER 05-1
Apprenez Python avec ChemTHEATER
Exécutez prepDE.py avec python3
1.1 Premiers pas avec Python
Collecter des tweets avec Python
Binarisation avec OpenCV / Python
3. 3. Programmation IA avec Python
Méthode Kernel avec Python
Non bloquant avec Python + uWSGI
Grattage avec Python + PhantomJS
Publier des tweets avec python
Conduisez WebDriver avec python
Utiliser mecab avec Python 3
[Python] Redirection avec CGIHTTPServer
Analyse vocale par python
Pensez à yaml avec python
Utiliser Kinesis avec Python
Premiers pas avec Python
Utiliser DynamoDB avec Python
Getter Zundko avec python
Gérez Excel avec python
Loi d'Ohm avec Python
Jugement des nombres premiers avec python
Exécutez Blender avec python
Résoudre des maths avec Python
Python à partir de Windows 7
Carte thermique par Python + matplotlib
Multi-processus de manière asynchrone avec python
Programmation Python avec Atom