java vs other

why is the running time of java so large compared to other languages,isn’t there any way to reduce it other than making the algorithm efficient!!

@zargus : Java is an interpreted language , so its slow , because the source code is not directly transformed to executable but instead to an intermediate byte code . So when your program is run , first the Java Virtual Machine (JVM ) is loaded which itself takes some time and then each line of your byte code is interpreted by the JVM and hence has its overhead .

The running time can be improved by JIT(Just In Time) compilation

You can read more about JIT here :

1 Like

@vineetpaliwal can it be implemented in solving problems in codechef?

1 Like

@zargus : No . Because in code chef you submit your source code , and it is up to them how to compiler and run your program .

Code Chef makes up for slow execution of Java program , by allowing twice the time for Java submissions as compared to what it gives to a C++ program .

1 Like

Java is slow because of indirection - it does not interact directly with OS. There are more reasons too.

As Vineet has mentioned already - Java code is given twice the TL (Time Limit). But still you need to take care of 2 things-

  1. Use faster Input/Output strategy - for this you can refer to this or look at code of top programmers coding in Java.
  2. Think asymptotically - if you are getting TLE, you may want to recheck your logic and try to reduce complexity of code.
1 Like
//