Is it possible that Java is faster than CPP? Why?

this is an algorithm for calculating palindromes less than 100,000 primes written with CPP .

-sharpinclude <iostream>
using namespace std;
int main()
{
    int input_num=100000;
    int pp_count=0;
    for(int each=2; each<=input_num; eachPP)
    {
        int factorization_lst=0;
        for(int factor=1; factor<=each; factorPP)
            if(each%factor==0&&!(factor>each/factor))
                factorization_lstPP;
        if(factorization_lst==1)
        {
            int antitone=0,each_cpy=each;
            while(each_cpy)
            {
                antitone=antitone*10+each_cpy%10;
                each_cpy/=10;
            }
            if(antitone==each)
            {
                pp_countPP;
                cout<<pp_count<<":"<<each<<endl;
            }
        }
    }
    return 0;
}

A slightly modified version of Java , with the addition of timing related parts.

public class main {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        int input_num = 100000;
        int pp_count = 0;
        for (int each = 2; each <= input_num; eachPP) {
            int factorization_lst = 0;
            for (int factor = 1; factor <= each; factorPP)
                if (each % factor == 0 && !(factor > each / factor))
                    factorization_lstPP;
            if (factorization_lst == 1) {
                int antitone = 0, each_cpy = each;
                while (each_cpy != 0) {
                    antitone = antitone * 10 + each_cpy % 10;
                    each_cpy /= 10;
                }
                if (antitone == each) {
                    pp_countPP;
                    System.out.println(pp_count + ":" + each);
                }
            }
        }
        System.out.println(System.currentTimeMillis() - start);
    }
}

execution result:
Codeblocks
eclipse
CPP230sJava124sCPP

:3Braspbian(java17s)CPPJavacodeblockseclipse(eclipse20123.8.1codeblocks201616.01)gcc-O2mainJavaJava


:
@Untitled(sf)JITJavaIDEIO
(shellCPP)


JITJava(797110)

JIT(1,2)JVMJavaCPP
JavaJIT


:
x86/x64WindowsLinuxCPPJavaarmAndroid7(6304GBAndroid 8.0root)IDE(Linux Deploy):ide.com/" rel="nofollow noreferrer">AIDE (Java)ide" rel="nofollow noreferrer">CIDE (CPPaarch64gcc7.2)

CIDECPP


-O3(-Os)

:()

CPP43s

Java37s

.....
()


:
Untitledclang(Raspbianapt install clang)
(Java)
:3m22s(202s)
-O2:3m05s(185s)(-O3-O2)

java


time...


:
Android()

(CPPJava)

:

MainActivity.java


Java.java


native-lib.cpp



:

:

  • Xposed30%

:
:android.os.Build

MODEL MANUFACTURER DISPLAY SDK_INT Java CPP
GT-I9300 samsung lineage_i9300-userdebug 7.1.2 NJH47F 0f9e26b899 25 192169 171928
Redmi 4A Xiaomi NJH47F 25 66009 31907
m2 Meizu Flyme 6.3.0.0A 22 37722 34654
A2 softwinner 2G 19 239865 202402
Redmi Note 3 Xiaomi OPM1.171019.018 27 22299 18105
TA-1041 HMD Global 00CN_1_34E 26 37310 20234
HTC 802t htc LRX22G release-keys 21 48211 125279

arm AndroidCPPJava

the CPU of this device is Snapdragon 600. < del > (how strange.) < / del >

another: I bought a orange pie zero plus two days ago, using Quan Zhi H5 . CPP 45s , java 70s .

all my arm devices have been tested. Can I come to the following conclusion?

in a small number of arm instruction set architecture devices, Java runs faster than CPP.

want to know why.

Mar.04,2021

I ran the code you gave me on the old school server. Cpp uses the time that comes with the system, and java uses the program's own result. The result is cpp's 25.125s user time, 25.130s wall clock time; java 25.240s user time, 25.250s wall clock time, self-proclaimed 25114 In order to eliminate the influence of the I _ cpp O operation, I commented out all the output of cpp and java except the last sentence of the java version, and tried again, and the result was pretty much the same. So I'm going to change 100, 000 to 1 million. I haven't finished running yet. I'll put the results up again when I'm free < del > ha < / del >

.

the above are very temporary and imprecise experimental results, so there is no time to do a large number of control experiments. I think that in some cases, the reason why java programs with identical code logic are faster than cpp programs or c programs should be the difference between compiler optimization and the implementation of individual operations, but I can't think of any other reason for the time being. No matter what language the program is written, the actual "running" must be the machine instruction, the body part compiled by cpp and c is a bunch of machine instruction sequence, and the java compiled is the machine instruction sequence of jvm (right? ), but you have to jvm to interpret the bytecode when executing, and you still have to execute the host machine instructions when the explanation is executed, but there seems to be a saying of JIT ? (not very familiar with java). So I believe that theoretically speaking, without using JIT , java is definitely not as fast as cpp and c (in the sense of "the same program", but this "same" is very difficult to define, as long as you understand it), because java still has to "explain" before it can be "executed". So I think it must be the compiler's pot, but I just feel that I don't have any experimental results.

can you try to turn off JIT and compare the results?

if you are interested, you can try different programs, simple, complex, and compare multiple programs (remember that java automatically skips some meaningless loops, such as idling loops, which should be noted). Among them, the simple program can disassemble the compiled files, analyze the theoretical execution efficiency, and find out the reasons for the differences.

< hr >

tried to run on raspberry pie 3B, and it turned out to be much slower than the main question, but java was indeed faster than cpp. Using the same code as the landlord, cpp ran for more than 4 minutes, and java ran for nearly 2 minutes of, quite interesting: D. Both cpp and java use the default compilation parameters, and the gPP version is 4.9.2, and the javac version is 1.8.0, the java version is 65, and the java version is 1.8.0_65-b17.

then I added the -O2 parameter to the cpp version, and the time was increased to more than 3 minutes and 40 seconds-- still not as fast as the java version of emmmmm

.

this is more amazing, so I tried another program-- I wrote a Fibonacci sequence evaluation myself-- which is comfortable this time. Cpp 1.6sMagna 3.4s, < del > more details later, first sweep the grave < / del > the following is the test code

< H2 > Recursive Fibonacci sequence < / H2 >

Test code

// test.cc
-sharpinclude <iostream>

using namespace std;

int fibo(int n)
{
    switch(n)
    {
        case 1: return 1;
        case 2: return 1;
        default: return fibo(n - 1) + fibo(n - 2);
    }
}

int main()
{
    cout << fibo(40) << endl;
    return 0;
}
// main.java
public class main {
  private static int fibo(int n) {
    switch(n) {
      case 1: return 1;
      case 2: return 1;
      default: return fibo(n - 1) + fibo(n - 2);
    }
  }

  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    System.out.println(fibo(40));
    System.out.println(System.currentTimeMillis() - start);
  }
}
// Makefile
main: test.cc main.java
    gPP test.cc -o test -O2
    javac main.java
  

I don't know anything. I liked it when I saw GGWP.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e49841-44d53.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e49841-44d53.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?