Is java language is slower than CPP for competitive programming

Choosing the right language is the most important factor when starting competitive programming. Usually, we choose a language with short syntax and very fast execution speed or a language we are familiar with, and we know all the working modes of that particular language, whether it is Java or C++.

Most programmers use C++ for competitive programming, and many older programmers turn to C++. The most acclaimed competitive programmers such as Gennady Korotkevich and Errichto, as well as many other programmers use C++ for competitive programming, but why Java handles a lot less competitive programming than C++? Let us understand this

1. Choosing the Wrong Class for I/O Operations or Using Bad Syntax

Compared with C++, the Java language is not very slow. There are two classes generally used for I/O operations in JAVA, Scanner class and BufferedReader class. Most programmers, especially beginners, use the Scanner class for I/O operations and avoid using the Buffered Reader class for I/O because of the long syntax. The internal parsing operation in the scanner class makes the execution of the program very slow, while the buffer reader class only reads the input and performs further analysis according to the needs of the operation. Using the Scanner class creates TLE problems for Java programs in competitive programming. Many articles have been published on how to avoid TLE in Java, namely "How to get rid of Java TLE problem" and "How does online refereeing work and how to avoid time limit exceeding problem"? and many more. In short, all articles recommend using the Buffered Reader class instead of the Scanner class for I/O operations.

 

import java.io.*;
import java.util.*;

class GFG {

public static void main(String[] args) throws NumberFormatException, IOException {
	
		// Reading the input and performing internal parsing
		Scanner scan = new Scanner(System.in);
			
		int n = scan.nextInt();
		
		// Reading the input as a Stream Using InputStreamReader
		BufferedReader Bd = new BufferedReader(new InputStreamReader(System.in));
		
		// converting the String into int datatype
		int m = Integer.parseInt(Bd.readLine());
	}
}

From the article How to create a Java snippet for competitive programming in VSCode? If you want to know how to slow down the execution speed of the Java language, please read the article Fast I/O in Java in Competitive Programming. The C++ language is also fast because it is very close to the machine and registers. This is why C++ is also used in embedded circuits instead of Java.

2. Compilation and Execution Process

Programming languages ??can be distinguished according to the execution and compilation process of their programs. Some languages ??are interpreted languages ??which means that the interpreter checks the syntax of a specific interpreted language (such as Python) line by line.

The compiled language directly converts the program into machine code. Therefore, these languages ??are much faster than interpreter languages. The interpreter spends very little time analyzing the source code. However, the total time to execute the process is much slower. Compared with compiled languages ??such as C or C++, this makes the execution speed of interpreted programming languages ??very slow. This is why Python is slower than C++ and Java.

Java is neither a compiled language nor an interpreted language. It lies withinside the center. Java is a platform-impartial language which makes it very famous amongst all of the programming languages. The Byte code makes it a platform-Independent language. This is the benefit of Java. It makes the execution of applications slower than C++ software due to the fact there aren't anyt any center operations that arise for execution and compilation like Java in C++. The motive for the sluggish execution of the software, there's a massive overhead to beginning the Java code if the digital device isn't always running.

3. Memory Consumption

The reminiscence intake of the Java packages is greater than the C++ program. (Refer to the above packages of Java and C++). We all realize that Java itself manages the reminiscence and wishes no specific intervention of the programmer. The rubbish collector itself guarantees that the unused area receives wiped clean and reminiscence may be freed whilst now no longer needed. So more rubbish collector takes more reminiscence to hint all of the reminiscence intake of the java program.

It is beneficial for the java programmer not to take care of memory management. It is automatically done by the JVM and garbage collector but the garbage collector is not present in C++. There is a chance of memory leak but it makes the C++ programs very light. 

Hence, the memory consumption of C++ programs is very less than compared to Java programs. As a result, it helps in the fast execution of the C++ programs.

 

  “If your weapon is sharp your chance to win the game increases”

 

Submit Your Programming Assignment Details