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.
endluse'\n'