python - xtensor 和 xsimd:提高还原性能
问题描述
我试图在减少操作(例如元素总和)上使用xtensor获得与 NumPy 相同的性能。
我启用xsimd进行并行计算,但它没有效果。
以下是基准代码:
#include <iostream>
#include "xtensor/xreducer.hpp"
#include "xtensor/xrandom.hpp"
#include <ctime>
using namespace std;
pair<double, double> timeit(int size, int n=30){
double total_clocks = 0;
double total_sum = 0;
for (int i=0;i<n;i++){
xt::xtensor<double, 1> a = xt::random::rand({size}, 0., 1.);
int start = clock();
double s = xt::sum(a, xt::evaluation_strategy::immediate)();
int end = clock();
total_sum += s; total_clocks += end-start;
}
return pair<double, double>(total_clocks/CLOCKS_PER_SEC/n, total_sum);
}
int main(int argc, char *argv[])
{
for (int i=5;i<8;i++){
int size = pow(10, i);
pair<double, double> ret = timeit(size);
cout<<"size: "<<size<< " \t " <<ret.first<<" sec\t"<<ret.second<<endl;
}
return 0;
}
并在启用和不启用xsimd和启用所有优化(-O3)的情况下编译它:
$ g++ -DXTENSOR_USE_XSIMD -O3 -march=native -I/home/--user--/install_path/include "./18. test speed 2.cpp" -o a && ./a
size: 100000 0.0001456 sec 1.49984e+06
size: 1000000 0.0013149 sec 1.50002e+07
size: 10000000 0.0125417 sec 1.49995e+08
$ g++ -O3 -march=native -I/home/--user--/install_path/include "./18. test speed 2.cpp" -o a && ./a
size: 100000 0.0001433 sec 1.49984e+06
size: 1000000 0.0012621 sec 1.50002e+07
size: 10000000 0.0124868 sec 1.49995e+08
顺便说一句,使用 numpy 的相同操作:
$ python bench.py
size: 100000 0.000030 sec
size: 1000000 0.000430 sec
size: 10000000 0.005144 sec
大约快 4 倍!
设置
- Ubuntu 18.04
- 酷睿 i7 处理器
- 最新版本的软件包
如何提高 xtensor 的性能?提前致谢))
解决方案
推荐阅读
- sql - 确定 Postgres 中所有数组之间的公共元素的数量
- neo4j - 我可以使用 Cypher 查询中的什么技术来仅获取基于每个节点的多个关系属性的路径
- email - 迁移到 Buster 后 SSMTP 失败
- ios - 如何为我的 CollectionView 单元格提供 onclick 功能?
- azure - 使用网络规则集对 Azure 容器注册表进行 Terraform 部署
- azure-cloud-shell - 从我的 Powershell 窗口运行 Azure Cloud Shell
- html - 同时具有两种定位的元素
- laravel - 我正在使用 Laravel、vue-router 和 MySQL 数据库创建一个 Web 应用程序
- mongodb - MongoDB,通过几个日期条件匹配数组中的子元素来查找文档
- java - (JAR 文件):java.lang.NoClassDefFoundError