Why Java is so slow?


  • 0
    P

    in this problem,i solve it with java,and my code is not satisfied me,so i read the discuss to find a better idea,and i think this solution is amazing which is write with C++

    	  if (n <= 2) return n;       // no need to deal with n<=2 case.
    	  int len = 2, itor = 2;
    	  while (itor < n) {
    	     if (A[itor] != A[len-2]) 
    	         A[len++] = A[itor];
    	        itor++;
    	    }
        return len;enter code here
    

    then i run this code on OJ,it takes 80ms run all the test sample.
    then i add one line code

    int n=A.length
    

    and i run it in java environment ,the OJ takes 404ms run all the test sample.
    why java vould be so slow??


  • 0
    G

    Java is always slower than C++ if the code is same.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.