AMD Athlon(tm) 64 FX-53 Processor
Memory: 8GB
gcc version 4.1.2 20080704 (Red Hat 4.1.2-51)
javac 1.6.0_20
测试结果
[~]$javac jmatrix.java
[~]$/usr/bin/time -p java jmatrix
java allsum=1.8658666E16
real 27.90
user 26.82
sys 0.17
[~]$g++ cmatrix.cpp
[~]$/usr/bin/time -p ./a.out
c++ allsum=1.86587e+16
real 70.89
user 69.99
sys 0.32
【g++ 优化】
[~]$g++ -O3 cmatrix.cpp
[~]$/usr/bin/time -p ./a.out
c++ allsum=1.86587e+16
real 28.90
user 28.74
sys 0.11
real 28.90
user 28.74
sys 0.11
测试代码
=====================jmatrix.java==========================
public class jmatrix {
final static int size=2000;
public static void main(String argv[]){
double x[];
double y[];
double m[][];
int i,j,k;
double sum;
double allsum;
try{
m=new double[size][size];
y=new double[size];
x=new double[size];
for(i=0;i<size;i++){
for(j=0;j<size;j++){
m[i][j]=i+j;
}
x[i]=i;
y[i]=0.0;
}
allsum=0.0;
for(k=0;k<size;k++){
for(i=0;i<size;i++){
sum = 0.0;
for(j=0;j<size;j++){
sum+=m[i][j]*x[j]+k;
}
y[i]=sum;
allsum+=sum;
}
}
}finally{
//delete[] m;
m=null;
x=null;
y=null;
System.gc();
}
System.out.println("java allsum="+allsum);
}
}
=====================cmatrix.cpp==========================
#include <iostream>
using namespace std;
#define size 2000
int main(int argc, char** argv){
double *x,*y;
double (*m)[size];
int i,j,k;
double sum,allsum;
m=new double[size][size];
y=new double[size];
x=new double[size];
for(i=0;i<size;i++){
for(j=0;j<size;j++){
m[i][j]=i+j;
}
x[i]=i;
y[i]=0.0;
}
allsum=0.0;
for(k=0;k<size;k++){
for(i=0;i<size;i++){
sum = 0.0;
for(j=0;j<size;j++){
sum+=m[i][j]*x[j]+k;
}
y[i]=sum;
allsum+=sum;
}
}
delete[] m;
delete[] x;
delete[] y;
cout<<"c++ allsum="<<allsum<<endl;
}
没有评论:
发表评论