比较Java和C++的运行速度
2015-05-11 21:57
225 查看
因为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++更快!
实验环境:硬件: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++更快!
相关文章推荐
- JAVA为什么没有C/C++运行速度快
- JAVA与C++运行原理比较
- JAVA与C/C++运行速度对比分析
- 实测java 与php运行速度比较
- java运行速度比C++慢的原因
- java运行速度比C++慢的原因
- 实测java 与php运行速度比较
- java和c/c++的运行效率,谁的比较高?
- 浅谈C++/JAVA/C#运行机制和执行效率
- java 和 C 代码运行效率的比较(整理)
- C++与Java比较
- Java,C#,C++在继承,覆盖和多态,抽象类等几个方面的比较归纳
- C/C++/JAVA中short,int,long等整型表示范围的比较
- C#与C++、Java之比较概览
- java运行长度10000的数组堆栈溢出,c++和Delphi都没有
- C++与Java比较(转)
- C++、Java和C#的特性比较
- 多态在 Java 和 C++ 编程语言中的实现比较
- c++/java/c# 几种编程语言的指针、引用比较
- C++与Java比较