+ 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
5 Answers
+ 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?
+ 13
Here is and some others tests:
StringConcat
https://code.sololearn.com/caHBtUN4Jcu8/#java
SpeedOfLoop:
https://code.sololearn.com/cBTOiMaccqW0/#java
ReverseString:
https://code.sololearn.com/cII0gSOZEKD6/#java
compare Numbers and Strings:
https://code.sololearn.com/cBFkvTEO8clK/#java
+ 12
Something about speed of input methods (Scanner, BufferedReader) :
https://www.cpe.ku.ac.th/~jim/java-io.html
+ 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
+ 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