Answer the question
In order to leave comments, you need to log in
Which is faster C++ or Java (string output to console)?
Hello! For fun, I launched the output code to the console 10,000,000 lines in Java and C ++, both launched from IED JetBraise.
Java Code
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10000000; i++) {
System.out.println("Number = " + i);
}
}
}
int main() {
for (int i = 0; i < 10000000; i++) {
printf("Number = %i \n", i);
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
Guys, I brought you something to eat. Opening Special Olympics.
:~/src/cpp_time_test$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 23
model name : Intel(R) Core(TM)2 Duo CPU T6600 @ 2.20GHz
stepping : 10
microcode : 0xa07
cpu MHz : 1200.000
cache size : 2048 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm dtherm
bogomips : 4389.29
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 23
model name : Intel(R) Core(TM)2 Duo CPU T6600 @ 2.20GHz
stepping : 10
microcode : 0xa07
cpu MHz : 1200.000
cache size : 2048 KB
physical id : 0
siblings : 2
core id : 1
cpu cores : 2
apicid : 1
initial apicid : 1
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm dtherm
bogomips : 4389.29
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
:~/src/cpp_time_test$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 13.04
Release: 13.04
Codename: raring
~/src/cpp_time_test$ java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) Server VM (build 24.51-b03, mixed mode)
~/src/cpp_time_test$ g++ --version
g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
time java Main
real 5m38.489s
user 0m30.184s
sys 0m56.864s
int main() {
for (int i = 0; i < 10000000; i++) {
printf("Number = %i \n", i);
}
return 0;
}
g++ -O2 origin_main.cpp -o origin_main.o
time ./orinig_main.o
real 5m54.260s
user 0m7.700s
sys 0m36.768s
#include <iostream>
using namespace std;
int main()
{
for (int i=0; i < 10000000; i++)
{
cout<<"Number = "<< i << '\n';
}
return 0;
}
g++ -O2 main.cpp -o main.o
time ./main.o
real 5m35.629s
user 0m8.424s
sys 0m37.408s
#include <iostream>
using namespace std;
int main()
{
static char buffer[1024*1024*4] ;
std::cout.rdbuf()->pubsetbuf( buffer, sizeof(buffer) ) ;
ios_base::sync_with_stdio(false);
for (int i=0; i < 10000000; i++)
{
cout<<"Number = "<< i << '\n';
}
return 0;
}
g++ -O2 main.cpp -o main.o
time ./main.o
real 5m35.555s
user 0m3.568s
sys 0m22.688
public class Main {
public static void main(String[] args) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(System.out, 2 << 22)) {
try (PrintWriter pr = new PrintWriter(bos, false)) {
int i = 0;
for (int j = 0; j < 10_000; j++) {
for (int k = 0; k < 1000; k++) {
pr.write("Number = " + i++ + '\n');
}
pr.flush();
}
}
}
}
}
time java -XX:LoopUnrollLimit=42000000 -XX:+AggressiveOpts -Xmx512m -Xms256m -XX:+OptimizeStringConcat -XX:CompileThreshold=200 Main
real 5m35.963s
user 0m4.408s
sys 0m23.456
Your validation is slightly wrong. Remove printf/coercion of a number to a string, make simple text output without formatting. And then maybe in Java "drain + number" works faster than formatted printf.
It's better not to print anything to the console if it's not necessary. This operation is very slow by itself.
The author is well done, he tore the ass of Java haters) Java has long been productive and not inhibitory. By the way, Java from PHP is ten times faster
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question