用 g++ 编译这个函数是可行的,但速度很慢。
void rota(double psir,double thetar,double phir,double xi,double yi,double zi,double *xf,double *yf,double *zf) {
*xf = xi*cos(phir)*cos(psir)+yi*(-sin(phir)*cos(thetar)*cos(psir)+sin(thetar)*sin(psir))+zi*(sin(phir)*sin(thetar)*cos(psir)+cos(thetar)*sin(psir));
*yf = xi*sin(phir)+yi*cos(phir)*cos(thetar)-zi*cos(phir)*sin(thetar);
*zf = -xi*cos(phir)*sin(psir)+yi*(sin(thetar)*cos(psir)+cos(thetar)*sin(phir)*sin(psir))+zi*(cos(thetar)*cos(psir)-sin(thetar)*sin(phir)*sin(psir));
return;
}
如果我计算一次中间值,然后调用它们,我的模拟运行得更快。
void rota(double psir,double thetar,double phir,double xi,double yi,double zi,double *xf,double *yf,double *zf) {
double cosf = cos(phir);
double sinf = sin(phir);
double cosp = cos(psir);
double sinp = sin(psir);
double cost = cos(thetar);
double sint = sin(thetar);
*xf = xi*cosf*cosp+yi*(-sinf*cost*cosp+sint*sinp)+zi*(sinf*sint*cosp+cost*sinp);
*yf = xi*sinf+yi*cosf*cost-zi*cosf*sint;
*zf = -xi*cosf*sinp+yi*(sint*cosp+cost*sinf*sinp)+zi*(cost*cosp-sint*sinf*sinp);
return;
}
为什么 g++ 不为我做这个优化?有没有办法让我更有效地做到这一点?
谢谢!