gpt4 book ai didi

r - Windows 版本的 R 包比 linux 版本慢得多

转载 作者:行者123 更新时间:2023-12-04 05:16:23 28 4
gpt4 key购买 nike

我正在运行 64 位 ubuntu。我有这个最小的测试包,我用来学习如何做这些事情(我正在关注这个 tutorial ,除了我在包中还有一些 c 代码)。

包在 linux 中构建/运行,所以我也开始让它在 windows 中运行。

我关注了这个 answer并使用 Uwe Ligges 维护的在线 Windows 包生成器来获取我的 package 的(工作)zip 版本.

现在,当我在 Windows (7-64) 上安装该 .zip 包时,小演示代码的运行速度比 linux 版本慢。因为慢了 30 倍。我怀疑差异总是那么大。
我想知道我做错了什么以及如何解决这个差距。

编辑:

这是源代码(这是一个最小的工作示例):

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <fstream>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <vector>

#include <Eigen/Dense>
#include <Eigen/LU>
#include <Eigen/SVD>

using namespace std;
using namespace Eigen;
using Eigen::MatrixXf;
using Eigen::VectorXf;


float median(VectorXf& x) {
int n=x.rows();
int half=(n+1)/2;
half--;
float med;
nth_element(x.data(),x.data()+half,x.data()+x.size());
if((n%2)==1){
med=x(half);
} else {
float tmp0=x(half);
float tmp1=x.segment(half+1,half-1).minCoeff();
med=0.5*(tmp0+tmp1);
}
return med;
}
VectorXf fx01(MatrixXf& x){
int p=x.cols();
int n=x.rows();
VectorXf Recept(n);
VectorXf Result(p);
for(int i=0;i<p;i++){
Recept=x.col(i);
Result(i)=median(Recept);
}
return Result;
}
extern "C"{
void mse(int* n,int* p,float* x,float* medsout){
MatrixXf x_cen=Map<MatrixXf>(x,*n,*p);
VectorXf MedsOut=fx01(x_cen);
Map<VectorXf>(medsout,*p)=MedsOut.array();
}
}

编辑2:

按照 cbeleites 的建议,我多次运行代码。这样做我发现
奇怪的是:函数的时序实际上和 linux 是一样的,除了什么时候
我打电话 apply()在调用我的函数之前——我一直在比较时间
的 colwise 中位数我的包计算到做 apply(X,2,median) 的时间——

好的,问题解决了。暂时。我现在仍然很好奇:为什么一个好的老式
调用 apply() (在一个巨大的矩阵 X 上)把事情搞砸了(system.time 从
90 秒到 3 秒)?

最佳答案

我想到的一种可能性是在不同机器上的计算可能有所不同是 BLAS(如果示例中存在线性代数计算)。

您是否在 Ubuntu(例如 libopenblas)上安装了优化的 BLAS,但在 Windows 上没有安装?

关于r - Windows 版本的 R 包比 linux 版本慢得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14214034/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com