这是一个 Python 和 Rust 混合编程的项目,主要目的是比较 Rust 和 Python 的性能差异。目前这个项目是 rshare,我们 在项目中通过 PYO3 和 Maturin 来将 Rust 代码包装到 Python 中,以使得用户可以通过 Python 代码来调用 Rust 写的函数。当然其中是有性能损失的。
目前在此版本中实现对 Rust 中调用 Python 代码,以使用利用 Rust 生成二进制文件的方式对 Python 代码进行加密的效果!
目前已经支持 64 位操作系统,包括 Windows,Linux,macOS 和 ARM64 架构的 Linux 系统。
通过 pip install rshare --upgrade -i https://pypi.org/simple
来安装 rshare
体验 Rust 的极致性能!
请先确认是否在本地安装以下依赖库
pip install numpy rshare talib
注意其中 talib 的安装需要参考:ta-lib-python
import time
import numpy as np
import rshare as rk
import talib
data_num = 100000000 # 调整此数值
data_np = np.random.rand(data_num)
timeperiod = 10
# Rust 和 C 对比
start_py = time.time()
result_talib = talib.SMA(data_np, timeperiod)
end_py = time.time()
print(f"基于 C 语言的 TA-Lib 耗时: {end_py - start_py} seconds")
start_py = time.time()
result_np_rs = rk.calculate_moving_average_rs(data=data_np, window_size=timeperiod)
end_py = time.time()
print(f"基于 Rust 的耗时: {end_py - start_py} seconds")
可以通过调整 data_num 数值来设置不同的数据量,从而比较性能差异!
maturin build # 会创建一个可以分发的轮子包,但不会安装它。
maturin build --release # 创建的轮子包是为发布准备的,具有更高的性能,但编译时间更长。
maturin develop # 非优化插入 Python 环境
maturin develop --release # 优化插入 Python 环境
import rshare as rk
rk.get_title("https://www.baidu.com")
目前主要是说明开发的软件版本:
- Python 3.12.1
- Rust 1.76
需要重新编译部分内容
- 推送到 main 分支
- 修改
Cargo.toml
中的版本号 - 打标签:
git tag v0.1.x
- 通过推送标签进行升级:
git push origin tag v0.1.x
- 基于原始镜像:
ghcr.io/rust-cross/manylinux2014-cross:aarch64
docker build -t jindaxiang/newopen .
docker tag local-image:tagname jindaxiang/new-repo:tagname
docker push jindaxiang/new-repo:tagname
用于 debug 代码,新增 .vscode 文件夹
利用 conda 创建虚拟环境,此处注意 Python 的版本,默认安装 conda 源中最新本的 Python
conda create -n rshare python
进入环境
conda activate rshare
pip install maturin
python -m pip install TA-Lib # 仅用于测试 talib