A nice nimporter that connects nim and python

Module that connects nim + python

I would like to introduce a module called nimporter because it was very moving. No, the introduction itself may be the nth decoction, but it seems that it is not well known. https://github.com/Pebaz/Nimporter In short, it's the guy who runs nim's ultra-fast code in python like an interpreter.

Here's an excerpt from the official example, but the nim code looks like this:

import nimpy

proc add(a: int, b: int): int {.exportpy.} =
    return a + b

The python code is as below

# Nimporter is needed prior to importing any Nim code
import nimporter, nim_math

print(nim_math.add(2, 4))  # 6

If you write it, ** it will be compiled automatically and imported **. No, it's really an interpreted language! It will be.

Promised speed comparison

Let's dare to calculate the Fibonacci sequence by recursion. The code for nim is below.

fibnim.nim


import nimpy
proc fib(n: int): float {.exportpy.}=
    if n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)

For python, see below.

fibpy.py


def fib(n: int) -> float:    
    if n == 1:
        return 0.
    elif n == 2:
        return 1.
    else:
        return fib(n - 1) + fib(n - 2)

... But it looks like Kusso. Let's import these two with python and compare them.

import fibpy
import nimporter, fibnim
from time import time
cycle = 36
t = time()
print(fibnim.fib(cycle))
print('Time of nim is {} sec'.format(str(time() - t)))
t = time()
print(fibpy.fib(cycle))
print('Time of python is {} sec'.format(str(time() - t)))

>>> 9227465.0
>>> Time of nim is 0.026133298873901367 sec
>>> 9227465.0
>>> Time of python is 3.1436727046966553 sec

It takes time to compile the first time, but after the second time, it is 100 times or more.

Is cython useless in the first place?

Not bad. Rather the first candidate. Cython has also become quite easy to use these days If you feed python with type hints to pure python mode, you can migrate from python without any changes. [^ ikou]

[^ ikou]: Actually, if you don't do your best in tuning, it won't be faster.

cythonize -i hoge.py

You can make a module like that, but is it troublesome to have to go back to bash once? Also, cython sometimes uses python, so I feel that there are times when it is unexpectedly troublesome when optimizing for shavings.

Recommended Posts

A nice nimporter that connects nim and python
Create code that outputs "A and pretending B" in python
How to write a metaclass that supports both python2 and python3
Python a + = b and a = a + b are different
This and that of python properties
[Python] return A [or / and] B
[Python3] I made a decorator that declares undefined functions and methods.
A python regular expression, str and unicode that are sober and addictive
[LINE Messaging API] Create a BOT that connects with someone with Python
A quick guide to PyFlink that combines Apache Flink and Python
A python script that deletes ._DS_Store and ._ * files created on Mac
[Python] A program that creates stairs with #
A memo with Python2.7 and Python3 on CentOS
Connect a lot of Python or and and
A python program that resizes a video and turns it into an image
[Python] Create a LineBot that runs regularly
A story about Python pop and append
A typed world that begins with Python
A program that plays rock-paper-scissors using Python
[Python] A game that uses regular expressions when, where, who, and what
[Python] A program that rounds the score
[Python, PyPDF2] A script that divides a spread PDF into two left and right
A Python script that reads a SQL file, executes BigQuery and saves the csv
[Python] I made a LINE Bot that detects faces and performs mosaic processing.
I want to exe and distribute a program that resizes images Python3 + pyinstaller
[Python] A program that finds the minimum and maximum values without using methods
[Python] A program that calculates the number of updates of the highest and lowest records
Organize python modules and packages in a mess
A memo that I wrote a quicksort in Python
[Python] A tool that allows intuitive relative import
Building a python environment with virtualenv and direnv
[Python / Tkinter] A class that creates a scrollable Frame
ffmpeg-Build a python environment and split the video
I wrote a class in Python3 and Java
Create a page that loads infinitely with python
A program that removes duplicate statements in Python
Why I'm a Java shop and start Python
Publish a Python module that calculates meteorological factors
Create a web map using Python and GDAL
"Python Kit" that calls a Python script from Swift
Launch a web server with Python and Flask
Python: Create a class that supports unpacked assignment
Let's write a Python program and run it
python memo-"if not A and B" was "if (not A) and B"
A Vim plugin that automatically formats Python styles
Create a Mac app using py2app and Python3! !!
A story about modifying Python and adding functions
[Python] A rough understanding of iterators, iterators, and generators
A discussion of the strengths and weaknesses of Python
A program that asks for a few kilograms to reach BMI and standard weight [Python]
Create a discord bot that notifies unilaterally with python (use only requests and json)
A python client application that downloads and deletes files from S3 by specifying a bucket
A script that combines your favorite python modules and binaries into one Lambda Layer
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
A note that runs an external program in Python and parses the resulting line
A story that Seaborn was easy, convenient and impressed
[Python] A program that counts the number of valleys
Python: Creating a virtual environment (venv), starting and stopping
Try creating a compressed file using Python and zlib
Build a python virtual environment with virtualenv and virtualenvwrapper
Let's write python code that parses go code and generates go code