test_zhufft.cpp

来自「几个FFT算法」· C++ 代码 · 共 80 行

CPP
80
字号
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "zhufft.h"

extern "C" double currTime();
extern "C" void fft( COMPLEX in[], COMPLEX out[], COMPLEX omega[],int n);
extern "C" void root(COMPLEX omega[],int n);

void  initTestData(COMPLEX data[],int len)
{
	int i;
	for (i=0;i<len;i++)
	{
		data[i].re=(double)(rand() % 1000);
		data[i].im=(double)(rand() % 1000);
	}
}

void testSpeed_zhufft(int maxLen)
{
	int len,c;
	COMPLEX *omega = new COMPLEX[maxLen];
	COMPLEX *p = new COMPLEX[maxLen];
	COMPLEX *f = new COMPLEX[maxLen];
	FILE *fp;
	double k,t1,t2,tmp1,tmp2,end;
	if (f==NULL)
	{
		printf("No enough memory to alloc\n");
	}
	fp=fopen("benchmark.txt","wt");
	if (fp==NULL)
		return;

	fprintf(fp,"    len  \tTotal time\tfft time \ttime/(n*log2(n)\n");
	for (len=64;len<=maxLen;len*=2)
	{
		t1=0.0;t2=0.0;
		c=0;
		
		initTestData(p,len);
		
		
		while (true)
		{
		
			tmp1=currTime();
			root(omega,len);
			
			tmp2=currTime();
			fft(f,p,omega,len);
			
			end=currTime();
			t1+=end-tmp1;
			t2+=end-tmp2;
			
			c++;
			if (t2>0.001)
				break;
		}
		t1/=c;
		t2/=c;
		k=t2/(len*log10(len)/log10(2));
		fprintf(fp,"%8d,\t%.8f \t%.8f\t%.12f\n",len,t1,t2,k);
	}
	fclose(fp);
	delete[] f;
	delete[] p;
	delete[] omega; 
}

int main()
{
	testSpeed_zhufft(2*1048576);
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?