Original Post
I am still in the learning process. I studied and programmed in Java from the beginning up until now is almost 2 years. Made several GUI applications, some games with threads and networking (my assignments). I am going to study C++ next, and I am interested in it because C++ is the language which is used primary in game industry, so maybe I'm gonna heavily investing time in it. I also did a little research on C++ vs Java as well as other languages, and I read other threads in this forum about this subject. Usually, the conclusion is C++ faster in most cases, except for lab benchmarks. However, I tried a little program, and C++ (using std::cout from iostream) is slower than Java, but faster than java using printf from C:
Java code:
Result: On my system, it completed in 42 secs.
C++ code:
Java code:
public class Main {
public static void main(String[] args) {
int count = 0;
for(int i= 0; i< 200000; i++)
{
System.out.println("Hello world!");
count++;
}
System.out.println("Count:"+count);
}
}
Result: On my system, it completed in 42 secs.
C++ code:
- With std::cout
#include <iostream>
using namespace std;
int main() {
int count = 0;
std::ios_base::sync_with_stdio(false);
for(int i = 1; i < 200000; i++)
{
cout << "Hello world!\n";
count++;
}
cout<<"Count:%d\n"<<count;
return 0;
}
Result: It took forever, so I stopped before it could even finish. I already turned off sharing with stdio from C to speed up significantly.- With printf
#include <iostream>
using namespace std;
int main() {
int count = 0;
for(int i = 1; i< 200000; i++)
{
printf ("Hello world!\n");
count++;
}
printf("Count:%d\n",count);
return 0;
}
Result: Less time to finish compare to Java.
So, should I stop using iostream and used the C stdio?
Also, some people argued that C++ is popular because of its legacy. But, is the language completed without its legacy? Will Java be Java when Java API does not exists anymore?
- With printf