+ 15

DISCUSSION : How to speed up the CODE? đŸ€”đŸ€”đŸ€”

Dear Sololearns, Solving the challenges, we often face with a time limit exceed. I think that this problem is present in all programming languages, but it seems to me that it is especially pronounced in Java. That made me think about how to speed up code execution. Please for your participation in the discussion, and for sharing experiences and advices about on the above. All languages are included. Edit: Generally, saving a few milliseconds or 1-2 seconds is not something significant to execute c

26th Oct 2017, 9:33 AM
LukArToDo
LukArToDo - avatar
5 Respostas
+ 14
Here are a few interesting tips to optimize your java code: https://www.javaworld.com/article/2077647/build-ci-sdlc/make-java-fast--optimize-.html In line with tips from the link, I made the following test: https://code.sololearn.com/cMxwhNmbE6qa/#java First I noticed that the first performance of the function is significantly slower than the following (Benchmark). So, it is necessary to repeat the test several times in order to get the best result. Can we use that info in our code? To make fake loop before true and larger loop?
26th Oct 2017, 9:20 AM
LukArToDo
LukArToDo - avatar
26th Oct 2017, 9:26 AM
LukArToDo
LukArToDo - avatar
+ 12
Something about speed of input methods (Scanner, BufferedReader) : https://www.cpe.ku.ac.th/~jim/java-io.html
26th Oct 2017, 9:21 AM
LukArToDo
LukArToDo - avatar
+ 12
Some conclusion for now for Java: 1. Don't use stream for large number of loop or recursion 2. Use StringBuilder for concatenation of strings 3. Don’t use Scanner for large number of inputs
26th Oct 2017, 9:25 AM
LukArToDo
LukArToDo - avatar
+ 11
I was delighted to discover the package java.util.stream That package I called "she" because the stream is so beautiful, so elegant, so sexy ... I was in love! After testing and research, I remained disappointed. Stream is so sloooow. Slow down the code over 5 times! “She” tricked me. For example, I testing speed of loop using diferent methods (for, while, IntStream, IntStream.parallel –I tried to speed up IntStream) and stayed sad: https://code.sololearn.com/cVqZH9475Bj0/#javaslow
26th Oct 2017, 9:21 AM
LukArToDo
LukArToDo - avatar