because life's too short for slow software
First example of making Python code faster.
Second example.
Notes about debugging C extensions.
Notes about translating C programs to Assembly.
Main reference for this topic: https://docs.python.org/3/extending/extending.html
At a minimum 2 files are needed:
cpalindromemodule.c
- C extension modulesetup.py
- script used to create Python extension
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static PyObject *cpalindrome_is_palindrome(PyObject *self, PyObject *args) {
}
static PyMethodDef CPalindromeMethods[] = {
{"is_palindrome", cpalindrome_is_palindrome, METH_VARARGS,
"check is string is palindrome"},
{NULL, NULL, 0, NULL}};
static struct PyModuleDef cpalindromemodule = {
PyModuleDef_HEAD_INIT, "cpalindrome",
"A C implementation of is_palindrome function", -1, CPalindromeMethods};
PyMODINIT_FUNC PyInit_cpalindrome(void) { return PyModule_Create(&cpalindromemodule); }
from distutils.core import setup, Extension
def main():
setup(name="cpalindrome",
version="1.0.0",
description="is_palindrome check in C",
ext_modules=[Extension("cpalindrome", ["cpalindromemodule.c"])])
if __name__ == '__main__':
main()
To install C extension you can run pip install -e .
in the directory with C code and setup.py
Since I already have assembly version of fibonacci and palindrome programs there was only one logical next step - writing Python extensions in pure assembly. After a couple of hours spent on that, I realized that the main issue is creating Python module data structures in Assembly and putting everything together. I tried to follow Writing Python Extensions in Assembly and I was able to make it work on Linux, but not on Mac which I'm currently working on. Leaving it for now, as I'm already satisfied with the state of this project.