It is a toy project for Python and Rust!
Project description
项目介绍
这是一个 Python 和 Rust 混合编程的项目,主要目的是比较 Rust 和 Python 的性能差异。目前这个项目是 rshare,我们 在项目中通过 PYO3 和 Maturin 来将 Rust 代码包装到 Python 中,以使得用户可以通过 Python 代码来调用 Rust 写的函数。当然其中是有性能损失的。
说明
目前已经支持 64 位操作系统,包括 Windows,Linux,macOS 和 ARM64 架构的 Linux 系统。
安装
通过 pip install rshare --upgrade -i https://pypi.org/simple
来安装 rshare
体验 Rust 的极致性能!
使用
import rshare as rk
# Rust 接口
import time
import talib
import rshare as rk
import numpy as np
from numba import njit
@njit
def cmma(bar_data, lookback):
# Initialize the result array.
n = len(bar_data)
out = np.array([np.nan for _ in range(n)])
# For all bars starting at lookback:
for i in range(lookback, n):
# Calculate the moving average for the lookback.
ma = 0
for j in range(i - lookback, i):
ma += bar_data[j]
ma /= lookback
out[i] = ma
# Subtract the moving average from value.
return out
# 生成数据
data_num = 10000000
data_np = np.random.rand(data_num)
timeperiod = 50
# Rust 实现
start_rs = time.time()
result_rs = rk.calculate_moving_average_rs(
data=data_np, window_size=timeperiod
)
end_rs = time.time()
print(f"Rust 实现: {end_rs - start_rs} seconds")
# Talib 实现
start_py = time.time()
result_talib = talib.SMA(data_np, timeperiod=timeperiod)
end_py = time.time()
print(f"Talib 实现: {end_py - start_py} seconds")
# Numba 实现
start_py = time.time()
result_numba = cmma(bar_data=data_np, lookback=timeperiod)
end_py = time.time()
print(f"Numba 实现: {end_py - start_py} seconds")
# 纯 Python 实现
start_py = time.time()
result_py = rk.calculate_moving_average_py(data=data_np, window_size=timeperiod)
end_py = time.time()
print(f"纯 Python 实现: {end_py - start_py} seconds")
# print(f"纯 Python 实现: 14 seconds")
# Rust 和 C 对比
start_py = time.time()
result_talib = talib.SMA(data_np, timeperiod)
end_py = time.time()
print(f"Talib took: {end_py - start_py} seconds")
start_py = time.time()
result_np_rs = rk.calculate_moving_average_rs(data_np, timeperiod)
end_py = time.time()
print(f"RSNP took: {end_py - start_py} seconds")
if result_talib[9999999].round(6) == result_np_rs[9999999].round(6):
print("equal")
else:
print("not equal")
# 定义测试函数
def test_fetch_function(func, url, times):
"""
测试函数
:param func: 函数
:param url: url
:param times: 测试次数
:return: None
"""
start_time = time.time() # 开始时间
for _ in range(times):
result = func(url=url)
print(result)
end_time = time.time() # 结束时间
total_time = end_time - start_time
print(
f"Function {func.__name__} executed {times} times, \
total time taken: {total_time:.2f} seconds"
)
address = "https://www.eastmoney.com/"
# 测试 fetch_name 函数
test_fetch_function(rk.fetch_name, address, 20)
# 测试 fetch_title 函数
test_fetch_function(rk.fetch_title, address, 20)
构建
maturin build
maturin develop --release
版本说明
目前主要是说明开发的软件版本:
- Python 3.11
- Rust 1.74.1
注意事项
需要重新编译部分内容
更新
- 推送到 main 分支
- 修改
Cargo.toml
中的版本号 - 打标签:
git tag v0.1.7
- 通过推送标签进行升级:
git push origin tag v0.1.7
镜像推送
- 基于原始镜像:
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
查看
贡献代码
关注 .vscode
用于 debug 代码,新增 .vscode 文件夹
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
rshare-0.1.16.tar.gz
(25.5 kB
view hashes)
Built Distributions
Close
Hashes for rshare-0.1.16-cp38-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1aa3fd4312e458a404d5af5b2da954f7b221d5598612570b3f720a201d6e94f8 |
|
MD5 | a865cea31650344a4254a3c2576ef3f5 |
|
BLAKE2b-256 | d6d8fb90bb30ff73f4eb8040e7cb23ae42057d1058e3d9e69771d975ac767fe8 |
Close
Hashes for rshare-0.1.16-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a84f4296fd9e45499bb9af09355a879f51d567eeb46f78f8aaa9e5d3c17f81a |
|
MD5 | 027d05b565111512342e593b3db0cfa9 |
|
BLAKE2b-256 | 71832b1f53135d012b95eddb583ab7c4a796a76cb9b97c511fcf6b59b9fc3a14 |
Close
Hashes for rshare-0.1.16-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf295be675e78ff426f520e6121c284c9430b5588b0b327bc9e867fa113f7cf5 |
|
MD5 | 62be1798eda6b9e7346e55758e5c81fb |
|
BLAKE2b-256 | 396ebfde8eafcc21f3a5c527692d297f943e14170b8dd90d78a5a301e028cf21 |
Close
Hashes for rshare-0.1.16-cp38-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b849c5166999e751c77db7d9e94eda016048f2c557d5221ca16dd307e1cf8cb |
|
MD5 | 2bc099f0fa1f0fca0d393826f38f9ebd |
|
BLAKE2b-256 | b2c2e27ee142a855a3e9296d25fb3eb51bb9c9c0317e3856ba20597a46c2c79f |
Close
Hashes for rshare-0.1.16-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eca340162d27d63d53e7626e7baf8adb7e7254a45e0b7c27fe30b9ba76cd2e01 |
|
MD5 | d532a0725d45a46c9810f5fd6cc3a355 |
|
BLAKE2b-256 | d98adaf8792f587b9105f64a762df5761d6f6a171e9b489172483eee883f00ce |