怎样提高调用数学函数的程序的性能

来源:计算机等级考试    发布时间:2012-08-29    计算机等级考试视频    评论


  附录 2 – C DFT 源程序
  #include <math.h>
  #define PI 3.1415926535897932384
  void dft(double x[],double a[],double phi[],int *m)
  {
  double y_re[NMAX], y_im[NMAX], t, s, term_re, term_im;
  int i,j,k,n=*m;
  for(i=0;i<n;++i) {
  y_re[i]=y_im[i]=0;
  }
  for(k=0;k<n;++k)
  {
  // compute y(k), k-th DFT output
  for(i=0;i<n;++i)
  {
  // compute i-th term of y(k):
  // x(k)*exp(-2*pi*I*(k-1)*(i-1)/n)
  // compute real and imaginary parts of i-th term
  // using exp(I*t)=exp(t)*(cos(t)+I*sin(t))
  t=-2.*PI*k*i/(double)n;
  term_re=x[i]*exp(t)*cos(t);
  term_im=x[i]*exp(t)*sin(t);
  // add term to sum
  y_re[k]+=term_re;
  y_im[k]+=term_im;
  }
  }
  // transform y to polar coordinates
  for(k=0;k<n;++k)
  {
  // compute amplitude of y(k)
  a[k]=sqrt(y_re[k]*y_re[k]+y_im[k]*y_im[k]);
  // compute phase of y(k)
  phi[k]=atan2(y_im[k],y_re[k]);
  }
  }
  // initialize input data
  void init(double a[],int *m)
  {
  int j,n=*m;
  for(j=0;j<n;++j)
  {
  a[j]=sin(1./sqrt((double)j+1.0));
  }
  }
  // Dummy function to use result, preventing compiler from
  // optimizing away the computation.
  void consume(double a[],double b[],double c[])
  {
  }

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答