求助:优化程序的运行效率,编了一个程序 运行时间太长
编写了一个程序计算数据,发现一个点的计算时间要2分钟左右,我有五十万个点要计算,耗时太长了,求助大牛帮我优化下 下边的程序,减少下运行时间。程序的主要功能是,输入一组数据到part[1][3],然后从三个0-90度独立变化的角a,b,c中选择一个组合使得O=misorientation(con[m].C,Mfcc,con[n].C,Mbcc)计算得到的数值最小,在计算每个abc组合的时候需要从 24*24个con[m].C 和 con[n].C的计算结果中选择最小的计算结果和别的abc组合进行比较。由于实际计算中需要很多组数据所以还有一定的 Osum=Osum+Omin; num++;等求平均值得操作,这些在计算单个数据的时候可以不考虑。在计算的时候abc和输入的数据都是转化为矩阵来计算的。
程序如下:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define Pi 3.14159
void matproduct(float a2[3][3],float b2[3][3],float c2[3][3]);
float misorientation(float con1[3][3],float fcc1[3][3],float con2[3][3],float bcc1[3][3]);
void transpose(float a3[3][3]);
void fccorientation (float par[][3],float fc[3]);
int main()
{
int i;
float part[1][3]={2.9976,46.554,37.132},fcc[3];
fccorientation(part,fcc);
printf("%f %f %f\n",fcc[0],fcc[1],fcc[2]);
return 0;
}
void fccorientation (float par[][3],float fc[3])
{float a1,b1,c1,a,b,c,o,p,q,Omin,Oaver,Osum,O;
float Oavermin=Pi; //abc组合当中的取向差的最小值
float Ocritical=1; //直接输入数据不要输入公式 计算得到的取向差需要符合的外部条件,可以通过计算结果进行调整
float Mfcc[3][3],Mbcc[3][3];
int num,i,m,n,x;
struct convert
{float C[3][3];};
struct convert con[24]=
{
{1,0,0,0,1,0,0,0,1},
{0,0,-1,0,-1,0,-1,0,0},
{0,0,-1,0,1,0,1,0,0},
{-1,0,0,0,1,0,0,0,-1},
{0,0,1,0,1,0,-1,0,0},
{1,0,0,0,0,-1,0,1,0},
{1,0,0,0,-1,0,0,0,-1},
{1,0,0,0,0,1,0,-1,0},
{0,-1,0,1,0,0,0,0,1},
{-1,0,0,0,-1,0,0,0,1},
{0,1,0,-1,0,0,0,0,1},
{0,0,1,1,0,0,0,1,0},
{0,1,0,0,0,1,1,0,0},
{0,0,-1,-1,0,0,0,1,0},
{0,-1,0,0,0,1,-1,0,0},
{0,1,0,0,0,-1,-1,0,0},
{0,0,-1,1,0,0,0,-1,0},
{0,0,1,-1,0,0,0,-1,0},
{0,-1,0,0,0,-1,1,0,0},
{0,1,0,1,0,0,0,0,-1},
{-1,0,0,0,0,1,0,1,0},
{0,0,1,0,-1,0,1,0,0},
{0,-1,0,-1,0,0,0,0,-1},
{-1,0,0,0,0,-1,0,-1,0},
};
for (a=0.0;a<=90;a=a+1)
for (b=0.0;b<=90;b=b+1)
for (c=0.0;c<=90;c=c+1)
{a1=a/180.0*Pi;
b1=b/180.0*Pi;
c1=c/180.0*Pi;
Mfcc[0][0]=(cos(a1)*cos(c1)-sin(a1)*sin(c1)*cos(b1));
Mfcc[0][1]=(sin(a1)*cos(c1)+cos(a1)*sin(c1)*cos(b1));
Mfcc[0][2]=sin(c1)*sin(b1);
Mfcc[1][0]=(-cos(a1)*sin(c1)-sin(a1)*cos(c1)*cos(b1));
Mfcc[1][1]=(-sin(a1)*sin(c1)+cos(a1)*cos(c1)*cos(b1));
Mfcc[1][2]=cos(c1)*sin(b1);
Mfcc[2][0]=sin(a1)*sin(b1);
Mfcc[2][1]=-cos(a1)*sin(b1);
Mfcc[2][2]=cos(b1);
Osum=0;
num=0;
for (i=0;i<1;i++)
{
o=par[i][0];
p=par[i][1];
q=par[i][2];
if ((o==0)&&(p==0)&&(q==0))
{
Omin=0;
} //对应if条件语句
else
{Mbcc[0][0]=cos(o)*cos(q)-sin(o)*sin(q)*cos(p); //由于初始化在程序的外边所以只能单独赋值
Mbcc[0][1]=sin(o)*cos(q)+cos(o)*sin(q)*cos(p);
Mbcc[0][2]=sin(q)*sin(p);
Mbcc[1][0]=-cos(o)*sin(q)-sin(o)*cos(q)*cos(p);
Mbcc[1][1]=-sin(o)*sin(q)+cos(o)*cos(q)*cos(p);
Mbcc[1][2]=cos(q)*sin(p);
Mbcc[2][0]=sin(o)*sin(p);
Mbcc[2][1]=-cos(o)*sin(p);
Mbcc[2][2]=cos(p);
Omin=Pi;
for (m=0;m<24;m++)
for (n=0;n<24;n++)
{ O=misorientation(con[m].C,Mfcc,con[n].C,Mbcc);
if (O<Omin)
Omin=O;
} //对应for条件语句
} //对应else 条件语句
Osum=Osum+Omin;
num++;
} //对应i层次的for 循环结束
Oaver=Osum/num;
if ((Oaver<=Oavermin)&&(Oaver<=Ocritical)) //在a,b,c的变化范围内寻找取向差最小的abc组合,同时最小的取向差还需要满足程序开始设定的标准Ocritical
{Oavermin=Oaver;
fc[0]=a;
fc[1]=b;
fc[2]=c;
}
}//对应最上边角度变化的for语句
printf("%f",Oavermin);
}//对应整个函数的结尾
float misorientation(float con1[3][3],float fcc1[3][3],float con2[3][3],float bcc1[3][3])
{int i;
float O1;
float vfb[3][3]={{0.7416,-0.6667,-0.0749},{0.6498,0.7416,-0.1667},{0.1667,0.0749,0.9832}}; //取向关系的矩阵描述
float a1[3][3],b1[3][3],c1[3][3],d1[3][3];
matproduct(vfb,con1,a1);
matproduct(a1,fcc1,b1);
matproduct(con2,bcc1,c1);
transpose(c1);
matproduct(b1,c1,d1);
//for(i=0;i<3;i++)
//printf("%f %f %f\n",d1[i][0],d1[i][1],d1[i][2]);
if((d1[0][0]+d1[1][1]+d1[2][2]-1)/2>=1)
O1=0;
else if((d1[0][0]+d1[1][1]+d1[2][2]-1)/2<=-1)
O1=3.14159;
else
O1=acos((d1[0][0]+d1[1][1]+d1[2][2]-1)/2);
//printf("%f",O1);
return(O1);
}
void matproduct(float a2[3][3],float b2[3][3],float c2[3][3]) //对矩阵进行乘法运算,结果存储到c矩阵中
{int i1,j1;
for (i1=0;i1<3;i1++)
for (j1=0;j1<3;j1++)
{c2[i1][j1]=a2[i1][0]*b2[0][j1]+a2[i1][1]*b2[1][j1]+a2[i1][2]*b2[2][j1];}
}
void transpose(float a3[3][3])//对矩阵进行转置运算
{float c3;
c3=a3[0][1];
a3[0][1]=a3[1][0];
a3[1][0]=c3;
c3=a3[0][2];
a3[0][2]=a3[2][0];
a3[2][0]=c3;
c3=a3[1][2];
a3[1][2]=a3[2][1];
a3[2][1]=c3;
}
[ 本帖最后由 zhht87 于 2013-8-16 18:16 编辑 ]