That’s Too Slow, Optimize It Again!

Optimizing A Piece Of Code 2 — Java

Different Insights On How To Increase Code Performance 2

JumperBot_
4 min readAug 20, 2022
Lightning Zap! The Ultimate Symbol Of High Performace!
Photo by Vlad Panov on Unsplash

Have you been trying to code non-stop again?
ave you wondered if your code is “fast” enough to beat the time limit?
Or have you ever wondered:

Can My Code Just Go *ZAP* Already?
— Probably You Done With~Uh… Asking yourself?

1 — Stop Trying To Loop It Again!*

A Big Loop Of Stairs!
Photo by Tine Ivanič on Unsplash

CPU/GPUs are rather very good at optimizing loops…
…But that doesn’t mean that it’s as fast as a one-line solution!

Remember no.2 from the first part?
This is yet another sequel for that.

Looping doubles/triples/… the number of instructions being executed!

A Terrible Example Of Looping

Above is one terrible example of using a for-loop.

Think if it can be solved without loops before trying to implement it!

If you can’t solve it without loops:

  • Check if there is an optimized built-in method for it.
  • Optimize the for loop itself by setting a personal instruction limit.

2 — Stop Using Methods That Do Too Much!

Doing Too Much, Too Busy!
Photo by Nick Fewings on Unsplash

Methods should always* do only one thing!
It’s a saying that never gets old.

An example of which is a built-in method called Long#parseLong().

Yes, this method is very well optimized and I shouldn’t be calling it out…
But it’s doing way too much!

This method is used to convert a String into a long by the specified radix.
But here’s all that it does:

  • Check if a String is null. (I’m okay with that)
  • Check if the radix is good. (I’m okay with that… if the radix isn’t 10)
  • Check the sign (+/-). (I’m okay with that)
  • Check if each and every character is a qualified number or not. (Hmm)
  • Append the current number to the result.
  • Append the sign to the result. (Could’ve been done earlier, but… MATH)
  • Return the result.

I know that it has to check if it is a number because it looks like it has to.
But it could’ve been given to another method instead!

You could easily optimize it by:

  • Making sure that the String will never be null.
  • Making sure that what you are going to pass is always a number.
  • Removing the radix check/s.
  • Removing the “is-this-a-valid-number?” check/s.
  • Making sure that the numbers are consistently using only 1 sign.
  • Removing the sign check/s.
  • Removing all the Exceptions. (Catching Exceptions are a bit expensive)

3 — Write Readable And Concise “Clean” Code.

Some Python Code That Is Not Anywhere Near Java!
Photo by Chris Ried on Unsplash

“Clean Code” doesn’t mean that it has clean spacing everywhere…
…It means that it only does what it has to do, elegantly.

Writing Clean Code has it’s own benefits:

  • You get to lower the read-to-write ratio. (10:1)
  • You get to know which methods need optimizing fast.
  • You get to know what the mission of the method is, but faster.
  • You don’t get irritated when optimizing it.
  • You get a tiny performance boost since Clean Code performs better!

4 — Don’t Micro-optimize Too Much!

A Relatively Small Car For The Age!
Photo by Jessica Knowlden on Unsplash

Micro-optimization is the act of optimizing even the smallest of details.

The reason not to do this too much is that it can have a very rough downside.
It can either give a performance boost or another steep bridge to conquer.

I recommend only doing this when desperation has brought you there.

5 —Spend Less Time Improving Performance.

Rest, Literal Rest.
Photo by Priscilla Du Preez on Unsplash

Spend less time in a coding session.
Getting a break is important for your cognitive machinery.

Spending less time doesn’t mean that you’ll set a time limit and just stop.
Spending less time means that you’ll rest, think better and then code.

Resting also helps you from falling into the void of micro-optimization.

There are a lot more that you can do to try and optimize your code…
…But I’ll have to end this over here so… Go! Go! Go! Code it out!

Want to see me procrastinate but most importantly code?
Go and find out if you’d like My YouTube Channel then!

Want to read another article?
Read — Regex-Patterns-Made-Easy — by yours truly.

--

--

JumperBot_

Hey-ya, I develop Android apps , Websites and some CLIs for fun like anyone else out there.