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