Enjoyably using embedded C++ code or dynamic libraries
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}')
- 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
Release history Release notifications | RSS feed
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.0.tar.gz
(5.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file easycpp-1.0.0.tar.gz.
File metadata
- Download URL: easycpp-1.0.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
901d8068095dbe21b7d9c9fa3ce465adbef57087eb5020a460e0636fefc82bd5
|
|
| MD5 |
45a0b8548064de6049e920f3364f443c
|
|
| BLAKE2b-256 |
80a295148c6a0ca0ffb7b35dec83da2ce28bdc10b8f3406b74bf1403c6bd1d1b
|
File details
Details for the file easycpp-1.0.0-py3-none-any.whl.
File metadata
- Download URL: easycpp-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66b3bf5f3911fe5e0b82cd171b05d69227536f51c06a869fd0c9b35ec256f182
|
|
| MD5 |
c159ff268b0629ce5bc0600a274d1726
|
|
| BLAKE2b-256 |
56c5b389c062967298c7a51330b5b057c7b603144b9b5fb3ed9cd310395e9d51
|