C/C++ VS. Fortran | |||
项目 | C/C++ | Fortran | 备注 |
软硬件环境 | 操作系统:Windows XP professional 2002 Service Pack 3 CUP:Intel® Core™2 Quad 2.66GHz 4核 内存:3.00GB | ||
编译环境 | VS2008 | Intel Fortran Compler Xe 2011+MKL | |
浮点型循环比较 | #include <time.h> #include <iostream> using namespace std; int main() { clock_t start, end; int i,j,k; double m; start = clock(); m=0.0; for (i=1;i<=1000;i++) { for (j=1;j<=1000;j++) { for (k=1;k<=1000;k++) { m=m+1.0; } } } end = clock(); printf("Time was: %f\n", (double)(end-start)/CLOCKS_PER_SEC); printf("Result is: %f\n", m); char a; cin>>a; return 0; } 耗时:1.125s | PROGRAM CPUTIME IMPLICIT NONE REAL time_begin, time_end INTEGER I, J, K REAL(8) M CALL CPU_TIME ( TIME_BEGIN ) M=0.0_8 DO I=1,1000 DO J=1,1000 DO K=1,1000 M=M+1.0_8 END DO END DO END DO CALL CPU_TIME ( TIME_END ) PRINT *, 'TIME WAS ', time_end - time_begin PRINT *, 'Result is ', M pause END PROGRAM CPUTIME 耗时:1.125s | 在release版本下比较 |
整型循环比较 (带数据转换) | #include <time.h> #include <iostream> using namespace std; int main() { clock_t start, end; int i,j,k; int m; start = clock(); m=0; for (i=1;i<=1000;i++) { for (j=1;j<=1000;j++) { for (k=1;k<=1000;k++) { m=m+1*2.0; } } } end = clock(); printf("Time was: %f\n", (double)(end-start)/CLOCKS_PER_SEC); printf("Result is: %d\n", m); char a; cin>>a; return 0; } 耗时:8.734s | PROGRAM CPUTIME IMPLICIT NONE REAL time_begin, time_end INTEGER I, J, K INTEGER M CALL CPU_TIME ( TIME_BEGIN ) M=0 DO I=1,1000 DO J=1,1000 DO K=1,1000 M=M+1*2_8 END DO END DO END DO CALL CPU_TIME ( TIME_END ) PRINT *, 'TIME WAS ', time_end - time_begin PRINT *, 'Result is ', M pause END PROGRAM CPUTIME 耗时:0.515625s | 在release版本下比较 |
矩阵乘法比较1 (矩阵大小1048576*80) | #include <time.h> #include <iostream> using namespace std; int main() { clock_t start, end; int i,j,k; const int rows=1024*1024; const int cols=80; const int count=rows*cols; float *x=new float[rows*cols]; float c[cols][cols]; for (i=0;i<count;i++){ x[i]=1.0f; } start = clock(); for (i=0;i<cols;i++) { for (j=0;j<cols;j++) { float sum=0; for (k=0;k<rows;k++){ sum+=x[k*cols+i]*x[k*cols+j]; } c[i][j]=sum; } } end = clock(); printf("Time was: %f\n", (double)(end-start)/CLOCKS_PER_SEC); printf("Result is: %f\n", c[1][1]); char a; cin>>a; return 0; } 耗时:165.735s | 不使用MKL库: PROGRAM CPUTIME IMPLICIT NONE real :: x(1024*1024,80) real :: c(80,80) real :: sum_t integer :: i,j,k integer,parameter:: count=1024*1024 REAL time_begin, time_end x=1 CALL CPU_TIME ( TIME_BEGIN ) do i=1,80 do j=1,80 sum_t=0_4; do k=1,count sum_t=sum_t+x(k,i)*x(k,j) end do c(i,j)=sum_t end do end do CALL CPU_TIME ( TIME_END ) print *,c PRINT *, 'TIME WAS ', time_end - time_begin pause END PROGRAM CPUTIME 耗时:8.25s | 在release版本下比较 |
矩阵乘法比较2 (矩阵大小1048576*80) | 转置矩阵后相乘: #include <time.h> #include <iostream> using namespace std; int main() { clock_t start, end; int i,j,k; const int rows=1024*1024; const int cols=80; const int count=rows*cols; float *x=new float[rows*cols]; float c[cols][cols]; for (i=0;i<count;i++){ x[i]=1.0f; } start = clock(); for (i=0;i<cols;i++) { for (j=0;j<cols;j++) { float sum=0; for (k=0;k<rows;k++){ sum+=x[i*rows+k]*x[j*rows+k]; } c[i][j]=sum; } } end = clock(); printf("Time was: %f\n", (double)(end-start)/CLOCKS_PER_SEC); printf("Result is: %f\n", c[1][1]); char a; cin>>a; return 0; } 耗时:23.407s | 使用MKL库: PROGRAM CPUTIME use mkl95_blas, only: gemm IMPLICIT NONE real :: x(1024*1024,80) real :: c(80,80) REAL time_begin, time_end x=1 CALL CPU_TIME ( TIME_BEGIN ) call gemm(x, x, c, 'T', 'N', 1.0, 0.0) CALL CPU_TIME ( TIME_END ) print *,c PRINT *, 'TIME WAS ', time_end - time_begin pause END PROGRAM CPUTIME 耗时:1.3125s | 在release版本下比较 |
总结 | 1、从单纯的循环操作来看C/C++与Fortran的性能不相上下,但是从数值计算方面来看Fortran比C还是有比较大的优势的,如果使用MKL那优势就更明显了; 2、从对矩阵操作的代码上来看Fortran比C要更简洁一些,但是在Fortran中调用函数的操作会比较复杂一些。 |
C/C++ VS. Fortran
最新推荐文章于 2023-06-26 11:20:18 发布