Code Monkey home page Code Monkey logo

try-pybind11's Introduction

try-pybind11

Prerequisites

  • Download and extract pybind11, put the files under external folder
  • Make sure that C++ compiler is installed
  • Make sure that Python is installed

Example to Use pybind11

Compile .cpp file to .so using pybind11.

# CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(example)

add_subdirectory(extern/pybind11-2.12.0)

pybind11_add_module(example example.cpp)
$ mkdir build && cd build
$ cmake ..
$ make

Then we can call C++ methods from python, e.g. add() method we just defined in C++.

$ build git:(main) ✗ ipython
In [1]: import example

In [2]: example.add(10, 15)
Out[2]: 25

Roughly Comparing the Performance of C++, Python and Numpy

Matrix multiplication is quite time consuming, and can be used for our testing. Implement our own matrix multiplication function with C++, and Python as well.

// matrix.h
class Matrix {
  public:
    static void Dot(const Matrix &A, const Matrix &B, Matrix &C);
}

// matrix.cpp
void Matrix::Dot(const Matrix &A, const Matrix &B, Matrix &C) {
  for (size_t i = 0; i < A.Rows(); i++) {
    for (size_t j = 0; j < B.Cols(); j++) {

      float sum = 0.0f;
      for (size_t k = 0; k < A.Cols(); k++) {
        sum += A.Get(i, k) * B.Get(k, j);
      }
      C.Set(i, j, sum);
    }
  }
}

Then run the script with specifying matrix size. 100 means a 100x100 matrix.

$ python compare.py --size=100
Numpy matrix multiplication: size: 100, time: 0.0004189014434814453
C++ matrix multiplication: size: 100, time: 0.006092071533203125
Python matrix multiplication: size: 100, time: 0.13921570777893066

After simple testing, the results reveal that C++ is much faster compared to Python. However, numpy spends the least time, with some unique optimizations.

References

关注了很久的 up HexUp 的视频:

try-pybind11's People

Contributors

keguigong avatar

Watchers

 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.