Code Monkey home page Code Monkey logo

workshop_cython's Introduction

By Quentin Rouvier, Philippe Cheype - Epitech Nice - 2023



Workshop Cython
Introduction and benchmarking

Epitech



Before starting

If you want to be present you need send an email to [email protected] with the following object:

[WSCYTHON] Nom Prénom

/!\ Put your real firstname and lastname

In the mail share a .zip or .tar.gz file with the contents of your repository.
/!\ Please run python3 setup.py clean before compressing your project

Any work you do needs to be in the email, if you don't do this or didn't advance into the workshop at all you will be marked absent.



Introduction

Cython is a language that makes writing C extensions for the Python language as easy as Python itself. In this workshop we will be using Cython to benchmark two different algorithms coded in Python and C(ython).



Setup

Requirements

  • Python 3
    • Check if installed with: python3 --version
  • Cython
    • Install with: pip3 install cython
  • DevTools
    • Install with:
      • For apt (Ubuntu, Debien...): sudo apt-get install python-dev #for pyhton2
        sudo apt-get install python3-dev
      • For yum (CentOS, RHEL...): sudo yum install python-devel
        sudo yum install python3-devel
      • For dnf (Fedora): sudo dnf install python-devel
        sudo dnf install python3-devel



Ex01: Just Cython

You will find two simple alogrithms made with python. Our job today is to make them work in C(ython).

Python

In the demo/ folder you will find a file named demo.py with the following content:

def pyloop():
    # ...

def pycount():
    # ...

Cython

In the demo/cython/ folder you will find a file named demo.pyx with the following content:

def cloop():
    # TODO: Fill this function

def ccount():
    # TODO: Fill this function

Setup

To compile cython code to C/C++ you will use cythonise. It will create a library (.so, for those unfamiliar with that extension it's similar to a .a library like the libmy.a that you should all be familiar with).

To compile the code you will use the following command:

pip3 install -e .

/!\ Make sure to keep any __init__.py files you see, they are empty but required for the setup.

Benchmark

In the current directory you will find the bench.py file:

import timeit

def main():
    # TODO: Fill this function

if __name__ == "__main__":
    main()

Using the timeit module call all 4 functions and print the results.



Ex02: Cython and C/C++

In this exercise we will rewrite the ccount() function with a more C-like syntax, create a new function underneath named betterccount():

def betterccount():
    # TODO: Fill this function

Take a look at the documentation here.

/!\ Make sure that you select Cython syntax and not Pure Python in the documentation. Those are two different approaches. Pure python is not necessary for our example.

Bemchmark

Let's go back to our bench.py file. Add the new betterccount() function and compare the results.



To go further

As a bonus if this subject interests you, you can look at the assembly code behind all this code.

Here is a website that will allow you to do that: godbolt.org,

You can paste your code in the left editor and look at the assembly operation in the right editor, here is an example with the pyloop() function

workshop_cython's People

Contributors

phil-chp avatar naashs-qr avatar alexandredfm avatar

Stargazers

Anaïs Parenti avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.