为什么(我的)Java比C ++快25倍?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么(我的)Java比C ++快25倍?相关的知识,希望对你有一定的参考价值。
当我想将它与C ++进行比较时,我正在玩Java中的一些FizzBuzz代码。
我在Java中运行下面的代码,在C ++中基本上使用相同的代码。
Java Eclipse IDE运行时:11秒
C ++代码阻止IDE运行时:202秒(比Java慢大约18倍)
C ++ Visual Studio IDE运行时:281秒(比Java慢大约25倍)
据我所知,我已经安装了所有这些IDE的最新版本。
为什么会这样?我所说的每一条信息都说C ++应该运行得更快。
Java代码:
public class Test {
public static void main(String []args) {
double fizzes = 0;
double buzzes = 0;
double fizzbuzzes = 0;
double normals = 0;
double iterations = 1000000;
for (int x = 0; x < iterations; x++) {
int index = 0;
String output = "";
if (x % 3 == 0) {
output = output + "Fizz";
index = 1;
}
if (x % 5 == 0) {
output = output + "Buzz";
index = index + 2;
}
if (output == "") { //x%5!=0 && x%3!=0
System.out.println(x);
normals++;
}
else {
System.out.println(output);
}
if (index == 3) {
fizzbuzzes++;
}
else if (index == 2) {
buzzes++;
}
else if (index == 1) {
fizzes++;
}
}
System.out.println("Normals " + (normals/iterations*100) + "%"); //percent
System.out.println("Fizzes " + (fizzes/iterations*100)+ "%");
System.out.println("Buzzes " + (buzzes/iterations*100)+ "%");
System.out.println("FizzBuzzes " +(fizzbuzzes/iterations*100)+ "%");
}
}
C ++代码:
#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
double fizzes = 0;
double buzzes = 0;
double fizzbuzzes = 0;
double normals = 0;
double iterations = 1000000;
for (int x = 0; x < iterations; x++) {
int index = 0;
string output = "";
if (x % 3 == 0) {
output = output + "Fizz";
index = 1;
}
if (x % 5 == 0) {
output = output + "Buzz";
index = index + 2;
}
if (output == "") { //x%5!=0 && x%3!=0
cout << x<< endl;
normals++;
}
else {
cout << (output)<< endl;
}
if (index == 3) {
fizzbuzzes++;
}
else if (index == 2) {
buzzes++;
}
else if (index == 1) {
fizzes++;
}
}
cout << "Normals " << (normals / iterations * 100) <<"%"<< endl; //percentage
cout << "Fizzes " << (fizzes / iterations * 100) << "%" << endl;
cout << "Buzzes " << (buzzes / iterations * 100) << "%" << endl;
cout << "FizzBuzzes " << (fizzbuzzes / iterations * 100) << "%" << endl;
return 0;
}
这只是按百分比给出了Fizz / Buzzes的数量。
这只是我的IDE配置的结果吗?
编辑:将C ++代码更改为您的建议
#include "stdafx.h"
#include <iostream>
#include <string>
int main()
{
double fizzes = 0;
double buzzes = 0;
double fizzbuzzes = 0;
double normals = 0;
double iterations = 1000000;
for (int x = 0; x < iterations; x++) {
int index = 0;
std::string output = "";
if (x % 3 == 0) {
output = output + "Fizz";
index = 1;
}
if (x % 5 == 0) {
output = output + "Buzz";
index = index + 2;
}
if (index == 3) {
fizzbuzzes++;
}
else if (index == 2) {
buzzes++;
}
else if (index == 1) {
fizzes++;
}
if (output == "") { //x%5!=0 && x%3!=0
//std::cout << x << "
";
normals++;
}
//else {
// std::cout << output << "
";
//}
}
std::cout << "Normals " << (normals / iterations * 100) <<"%"<< "
"; //percentage
std::cout << "Fizzes " << (fizzes / iterations * 100) << "%" << "
";
std::cout << "Buzzes " << (buzzes / iterations * 100) << "%" << "
";
std::cout << "FizzBuzzes " << (fizzbuzzes / iterations * 100) << "%" << "
";
int z;
std::cin >> z; //so i can see the values
return 0;
}
还可以在发布模式下构建,在IDE之外,它可以立即运行。看起来像打印需要很多时间。
这很有意思,我在我的PC上测试它,结果是一样的。
然后我尝试在java和c ++(使用-O3发布)中禁用打印,两者的时间使用时间<3秒。
所以问题是每次都是cout flush,但java在IDE中运行,IDE会缓冲所有输出,所以这应该是java比c ++更快的原因。
然后我尝试使用控制台运行java,并且比在IDE中运行需要更长的时间。
这应该与https://stackoverflow.com/users/3807729/galik将c ++ ouputput重定向到/ dev / null的测试一致
然后我将java代码更改为缓冲编写器,IDE内部的时间缩短为5秒。
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
public class Test {
public static void main(String []args) throws Exception{
double fizzes = 0;
double buzzes = 0;
double fizzbuzzes = 0;
double normals = 0;
double iterations = 1000000;
BufferedWriter sout = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(java.io.FileDescriptor.out),"ASCII"), 1024);
for (int x = 0; x < iterations; x++) {
int index = 0;
StringBuilder output = new StringBuilder();
if (x % 3 == 0) {
output.append("Fizz");
index = 1;
}
if (x % 5 == 0) {
output.append("Buzz");
index = index + 2;
}
if (output.length()==0) { //x%5!=0 && x%3!=0
sout.write(String.valueOf(x));
sout.write("
");
normals++;
}
else {
sout.write(output.toString());
sout.write("
");
}
if (index == 3) {
fizzbuzzes++;
}
else if (index == 2) {
buzzes++;
}
else if (index == 1) {
fizzes++;
}
}
System.out.println("Normals " + (normals/iterations*100) + "%"); //percent
System.out.println("Fizzes " + (fizzes/iterations*100)+ "%");
System.out.println("Buzzes " + (buzzes/iterations*100)+ "%");
System.out.println("FizzBuzzes " +(fizzbuzzes/iterations*100)+ "%");
}
}
以上是关于为什么(我的)Java比C ++快25倍?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 C++ 代码比 LeetCode 上的 C 代码慢三倍? [关闭]
世界上最快的内存数据库横空出世,比 Redis 快 25 倍,Star 数飙升,杀疯了!