B
B
Boniface2015-04-04 20:00:38
Java
Boniface, 2015-04-04 20:00:38

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

C++ code
int main() {

    for (int i = 0; i < 10000000; i++) {

        printf("Number = %i \n", i);
    }

    return 0;
}

To my surprise, the java code is faster. How can this be?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
angry_cellophane, 2015-04-05
@angry_cellophane

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

The measurements were carried out only a couple of times, for laziness, so there is some error. Please forgive and understand.
Java:
time java Main
real   5m38.489s
user   0m30.184s
sys   0m56.864s

C++
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

Putting on moccasins of speed:
#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

Let's try the magic:
#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

Let's add firewood stoves to the Java firebox:
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

Let's stop there, because it can go on indefinitely.
Conclusion: an artificial test is not an indicator, and everything is complicated.
The most important conclusion: it is not appropriate for a well-mannered gentleman to measure speed, size and quantity.

G
GavriKos, 2015-04-04
@GavriKos

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.

A
asd111, 2015-04-05
@asd111

It's better not to print anything to the console if it's not necessary. This operation is very slow by itself.

O
olexandr7, 2015-04-04
@olexandr7

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

I
iv_k, 2015-04-06
@iv_k

where is your c++?
you have C
in C++ you need cout <

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question