Skip to main content

Using embedded C++ code in .py

Project description

Description

To use C/C++ conveniently in Python

Functional Features

  • Support for embedding C++ functions in .py files: It is convenient to use embedded C++ functions instead of Python functions to accelerate code execution.
  • Calling dynamic libraries: Similar to ctypes.CDLL

Dependencies

  • Python >= 3.7
  • pip

Installation

$ pip install easycpp

Usage Examples

Use the easycpp module to insert C++ code in a .py file. test.py:

import sys, os
from easycpp import easycpp


cpp = easycpp('''
#include <vector>
using namespace std;

extern "C" int sieve(int n);

int sieve(int n) {
    vector<bool> prime(n + 1, true);
    prime[0] = prime[1] = false;

    for (int i = 2; i * i <= n; ++i) {
        if (prime[i]) {
            for (int j = i * i; j <= n; j += i) {
                prime[j] = false;
            }
        }
    }

    int rn = 0;
    int rmax = 0;
    for (int i = 2; i <= n; ++i) {
        if (prime[i]) rn++,rmax=i;
    }
    return rn;
}


''')


n = 10**6
rn = cpp.sieve(n)
print(f'count:{rn}')
  1. Run like normal Python code:
$ python3 test.py

Performance

Achieving a 20x speedup in prime number calculation.

output for test/testeasycpp.py:

python:sieve(1000000)
count:78498
cpp: sieve(1000000)
count:78498
python:sieve(1000000)
1. : 41969.04803393409  microseconds
2. : 40655.971970409155  microseconds
3. : 40405.491017736495  microseconds
4. : 40510.52400609478  microseconds
5. : 40443.39497340843  microseconds
cpp: sieve(1000000)
1. : 1861.8699978105724  microseconds
2. : 1843.6360405758023  microseconds
3. : 1847.2519586794078  microseconds
4. : 1861.9759939610958  microseconds
5. : 1847.1599905751646  microseconds

Attention

Must specify parameter func_signatures on Windows.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easycpp-1.0.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easycpp-1.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file easycpp-1.0.1.tar.gz.

File metadata

  • Download URL: easycpp-1.0.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for easycpp-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6d94b3d3636b6947f4204f170a6c43e1719d4ef85a739fadfe2beaa58bd7b990
MD5 86f5903ccec491daf527e87d0a93bfd1
BLAKE2b-256 242483595a02bb751a77df04d5ddab37d974d034461d16d5b7068ceb64f4a900

See more details on using hashes here.

File details

Details for the file easycpp-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: easycpp-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for easycpp-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 56e0ff84386b15033608166152bddd964adec9eaa84eced24df46f0acca97f90
MD5 e7e7b8a5fe7ecebef12c759561f71b07
BLAKE2b-256 c78ae3c1611b716dea3c00a21b399cba533c0d41c85a49f9173244a4ea61e7ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page