2

I was playing around with some FizzBuzz code in Java when i wanted to compare it to C++.

I ran the Code below in Java and essentially the same Code in C++.

Java Eclipse IDE runtime: 11sec

C++ Code Blocks IDE runtime: 202sec (about 18 times slower than Java)

C++ Visual Studio IDE runtime: 281sec (about 25 times slower than Java)

I have the newest version installed of all of these IDEs as far as i know.

Why is this happening? Every information i have says that C++ should run faster.

Java Code:

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++ Code:

#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; } 

This just gives the count of Fizz/Buzzes in percentage.

Is this just a result of my IDE configurations?

EDIT: changed the C++ code to your suggestions

#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 << "\n"; normals++; } //else { // std::cout << output << "\n"; //} } std::cout << "Normals " << (normals / iterations * 100) <<"%"<< "\n"; //percentage std::cout << "Fizzes " << (fizzes / iterations * 100) << "%" << "\n"; std::cout << "Buzzes " << (buzzes / iterations * 100) << "%" << "\n"; std::cout << "FizzBuzzes " << (fizzbuzzes / iterations * 100) << "%" << "\n"; int z; std::cin >> z; //so i can see the values return 0; } 

Also build in Release mode and outside of the IDE it runs pretty much instantly. Seems like printing takes alot of time.

18
  • 8
    How did you compile your C++? Did you use any optimization flags? It's pointless to discuss speed if your compiler is avoiding most optimizations and emitting stuff for debugging purposes. Commented Mar 4, 2018 at 14:30
  • 2
    Pehaps you did not build you program in Release mode, or otherwise built it without any optimization. You should also try to run your code outside your IDE - you program produces a lot of text output, and some IDEs can take a whole lot of time displaying all that output, instead of letting your program run as fast as it can. (Your C++ program takes 5 seconds to complete on my machine..) Commented Mar 4, 2018 at 14:33
  • 4
    The first step: turn that off: sync_with_stdio Commented Mar 4, 2018 at 14:34
  • 5
    And instead of endl use '\n' Commented Mar 4, 2018 at 14:35
  • 2
    std::endl is evil Commented Mar 4, 2018 at 14:39

1 Answer 1

7

This is quite interesting, I test this in my PC, it is the same result.

Then I try to disable the print in both java and c++ (release with -O3), the time usage is <3 seconds for both.

So the problem is cout flush every time, but java is running inside a IDE, the IDE will buffer all the output,so this should be the reason why java is faster than c++.

Then I try to run java with console, and it take much longer than running inside IDE.

This should be align with the test of redirect the c++ ouputput to /dev/null by https://stackoverflow.com/users/3807729/galik

Then I change the java code to a buffered writer, the time reduced to 5 seconds inside IDE.

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("\r\n"); normals++; } else { sout.write(output.toString()); sout.write("\r\n"); } 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)+ "%"); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.