快速而轻量级的无符号32位整数集。
pyroaring的Python项目详细描述
32位整数的一种高效、轻量级有序集合。 这是c库CRoaring的python包装器。
示例
您可以使用位图,几乎与代码中的经典python集相同:
frompyroaringimportBitMapbm1=BitMap()bm1.add(3)bm1.add(18)bm2=BitMap([3,27,42])print("bm1 = %s"%bm1)print("bm2 = %s"%bm2)print("bm1 & bm2 = %s"%(bm1&bm2))print("bm1 | bm2 = %s"%(bm1|bm2))
输出:
bm1 = BitMap([3, 18]) bm2 = BitMap([3, 27, 42]) bm1 & bm2 = BitMap([3]) bm1 | bm2 = BitMap([3, 18, 27, 42])
从PYPI安装
注意:这个安装方法需要一个最近的c编译器,比如gcc。
支持的系统:
- Linux和MacOS:Python2.7或Python3.4或更高版本。
- Windows:Python3.4或更高版本。
要在本地帐户上安装pyraring,请使用以下命令:
pip install pyroaring --user
对于系统范围的安装,请使用以下命令:
pip install pyroaring当然,后者可能需要超级用户权限(考虑前缀)。 命令由sudo。
如果要使用Python3,并且系统默认使用Python2.7,则 可能需要调整以上命令,例如将pip替换为pip3。
从车轮安装
Github上为每个版本发布了几个轮子: https://github.com/Ezibenroc/PyRoaringBitMap/releases
从控制盘安装应该是最简单的,因为不需要C编译器。但是,性能可能更低。请注意 你必须选择正确的方向盘,这取决于你的系统。
例如,要在linux上为python安装pyroaringversion0.2.1forpython3.6
pip install --user https://github.com/Ezibenroc/PyRoaringBitMap/releases/download/0.2.1/pyroaring-0.2.1-cp36-cp36m-linux_x86_64.whl
手动编译/安装
例如,如果你想自己编译(安装)pyoraring 若要修改cython源或因为您没有pip,请执行以下操作 这些步骤。
注意,python包Cython是必需的。您可以安装为:
pip install --upgrade setuptools -user pip install cython --user
克隆此存储库。
git clone https://github.com/Ezibenroc/PyRoaringBitMap.git cd PyRoaringBitMap git submodule init && git submodule update
在本地构建pyroaring,例如测试您创建的新功能。
python setup.py build_ext -i
在MacOS上,由于setuptools将-arch x86_64 -arch i386添加到编译器命令中,这可能会与-march=native标志冲突,因此可能会失败并出现错误。可以通过设置archflags标志覆盖此行为:
ARCHFLAGS="" python setup.py build_ext -i
然后您可以测试新代码:
pip install hypothesis --user
python test.py # run the tests, optional but recommended
安装pyroaring(如果没有pip,请使用此选项)。
python setup.py install # may require superuser rights, add option --user if you wish to install it on your local account
包装烤焦。
python setup.py sdist
pip install dist/pyroaring-0.1.?.tar.gz # optionnal, to install the package
造一个轮子。
python setup.py bdist_wheel
对于以上所有命令,可以使用两个环境变量来控制编译。
- DEBUG=1以调试模式生成pyroaring。
- ARCHI=<cpu-type>为给定平台构建pyroaring。平台可以是任何关键字 给gcc的-march选项(参见 documentation)。 请注意,不支持从64位体系结构交叉编译32位体系结构。
使用示例:
DEBUG=1ARCHI=x86-64 python setup.py build_ext
基准
Pyroaring与内置的set和其他实现进行比较:
- 一种叫python-croaring
- 一个叫roaringbitmap
- 一个名为sortedcontainers 的ordered sets的python实现
脚本quick_bench.py测量不同集合的时间 操作。它使用随机生成的大小为1e6和密度的集合 0.125。对于每个操作,30次测试的平均时间(秒) 是有报道的。
结果是通过:
- CPU英特尔至强CPU E5-2630 v3
- cpython 3.5.3版
- GCC版本6.3.0
- cython版本0.28.3
- 纵火犯 dcf448a
- python croaring提交 3aa61dd
- RoaringBitmap提交 502d78d
- SortedContainers提交 7d6a28c
operation | pyroaring | python-croaring | roaringbitmap | set | sortedcontainers |
---|---|---|---|---|---|
range constructor | 3.09e-04 | 1.48e-04 | 8.72e-05 | 7.29e-02 | 2.08e-01 |
ordered list constructor | 3.45e-02 | 6.93e-02 | 1.45e-01 | 1.86e-01 | 5.74e-01 |
list constructor | 1.23e-01 | 1.33e-01 | 1.55e-01 | 1.12e-01 | 5.12e-01 |
ordered array constructor | 5.06e-03 | 6.42e-03 | 2.89e-01 | 9.82e-02 | 3.01e-01 |
array constructor | 1.13e-01 | 1.18e-01 | 4.63e-01 | 1.45e-01 | 5.08e-01 |
element addition | 3.08e-07 | 8.26e-07 | 2.21e-07 | 1.50e-07 | 1.18e-06 |
element removal | 3.44e-07 | 8.17e-07 | 2.61e-07 | 1.78e-07 | 4.26e-07 |
membership test | 1.24e-07 | 1.00e-06 | 1.50e-07 | 1.00e-07 | 5.72e-07 |
union | 1.61e-04 | 1.96e-04 | 1.44e-04 | 2.15e-01 | 1.11e+00 |
intersection | 9.08e-04 | 9.48e-04 | 9.26e-04 | 5.22e-02 | 1.65e-01 |
difference | 1.57e-04 | 1.97e-04 | 1.43e-04 | 1.56e-01 | 4.84e-01 |
symmetric diference | 1.62e-04 | 2.01e-04 | 1.44e-04 | 2.62e-01 | 9.13e-01 |
equality test | 7.80e-05 | 7.82e-05 | 5.89e-05 | 1.81e-02 | 1.81e-02 |
subset test | 7.92e-05 | 8.12e-05 | 8.22e-05 | 1.81e-02 | 1.81e-02 |
conversion to list | 4.71e-02 | 2.78e-01 | 4.35e-02 | 5.77e-02 | 5.32e-02 |
pickle dump & load | 4.02e-04 | 6.27e-04 | 5.08e-04 | 2.41e-01 | 5.75e-01 |
“naive” conversion to array | 5.12e-02 | 2.92e-01 | 4.75e-02 | 1.20e-01 | 1.18e-01 |
“optimized” conversion to array | 1.27e-03 | 3.40e-02 | nan | nan | nan |
selection | 1.77e-06 | 5.33e-05 | 1.14e-06 | nan | 1.64e-05 |
contiguous slice | 9.38e-05 | 9.51e-05 | 6.99e-05 | nan | 2.04e-02 |
slice | 2.88e-03 | 3.04e-01 | 1.00e-01 | nan | 4.74e-01 |
small slice | 8.93e-05 | 3.00e-01 | 3.60e-03 | nan | 1.79e-02 |