测试一下这个程序的运行时间
请分别用下列选项或编译器编译下面的程序,并分别给出运行时间。
1. icl /O2
2. icl /O2 /Qvec-
3. cl /O2
参考运行平台:Core 2 Duo CPU, Win Xp, Visual Studio 2005/2008, Intel(R) C++ Compiler 11.1.065
我会在一周后给出我的测试结果,大家也可以考虑一下同样的程序在不同的编译器和选项下,运行时间会有多大差别,原因在哪里呢?
// Simple minded matrix multiply
#include <stdio.h>
#include <time.h>
#define NUM 1024
static double a[NUM][NUM], b[NUM][NUM], c[NUM][NUM];
//routine to initialize an array with data
void init_arr(double row, double col, double off, double a[][NUM])
{
int i,j;
for (i=0; i< NUM;i++) {
for (j=0; j<NUM;j++) {
a[i][j] = row*i+col*j+off;
}
}
}
// routine to print out contents of small arrays
void print_arr(char * name, double array[][NUM])
{
int i,j;
printf("\n%s\n", name);
for (i=0;i<NUM;i++){
for (j=0;j<NUM;j++) {
printf("%g\t",array[i][j]);
}
printf("\n");
}
}
// matrix multiply routine
void multiply_d(double a[][NUM], double b[][NUM], double c[][NUM])
{
int i,j,k;
double temp;
for(i=0;i<NUM;i++) {
for(j=0;j<NUM;j++) {
for(k=0;k<NUM;k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
}
main()
{
clock_t start, stop;
int num;
// initialize the arrays with data
init_arr(3,-2,1,a);
init_arr(-2,1,3,b);
//start timing the matrix multiply code
printf("NUM:%d\n",NUM);
start = clock();
multiply_d(a,b,c);
stop = clock();
// print simple test case of data to be sure multiplication is correct
if (NUM < 5) {
print_arr("a", a);
print_arr("b", b);
print_arr("c", c);
}
// print elapsed time
printf("Elapsed time = %lf seconds\n",
((double)(stop - start)) / CLOCKS_PER_SEC);
}