您的位置:首页 > 编程语言 > C语言/C++

比较Java和C++的运行速度

2015-05-11 21:57 162 查看
因为Java是解释执行的,所以,几乎所有的教科书都说,Java比C++运行速度慢!可是,实际上,是这样的吗?实践是检验真理的唯一标准。

实验环境:硬件:Intel i5 3230M双核处理器,2.6GHz 4GB DDR3内存 软件:Linux Ubuntu14.04 / Codeblocks / Eclipse

C++ code:

#include <iostream>

#include<stdlib.h>

#include<time.h>

using namespace std;

/*

* Test C++ running speed

*/

int main()

{

int i,j;

double d =0;

int limit =6;

clock_t start , finish;

// begins

start =clock();

for(i=0;i<limit*10000;i++)

for(j=0;j<10000;j++)

d = d + 0.001;

finish =clock();

// output

double time = ( finish - start ) * 1000.0 / CLOCKS_PER_SEC ;

cout<<"Time used:"<<time<<" ms"<<endl;

return 0;

}

实验结果: 1780ms(平均值)

Java code:

/*

* This program aims to test Java running speed !

*/

public class Test {

public static void main(String[] args) {

int i,j;

double d =0;

int limit =6;

long start , finish ;

// begin testing

start =System.currentTimeMillis();

for(i=0;i<limit*10000;i++)

for(j=0;j<10000;j++)

d = d + 0.001;

finish =System.currentTimeMillis();

long time = finish - start;

//output

System.out.println("Time used:"+time+" ms");

System.out.println("D value:"+d );

}

}

结果:

580ms(平均值)

关于实验结果的详细解释,参考:
http://blog.csdn.net/yongzhewuwei_2008/article/details/1387476
总结:综合考虑,Java比C++更快!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: