Codementor Events

Code inside - Improve the performance - session 1

Published May 20, 2021
Code inside - Improve the performance - session 1

In the program, we are use the class or library from the framework and don't care about the inside.
To store of the books, the ArrayList of books is created
To store of the orders, the ArrayList of orders is created
To store of the customers, the ArrayList of orders is created

Then to choose one of the books, the indexOf method is called
To remove the books, the remove method is called
To add a new book, the insert method is called
To sort the book, the Collections.sort method is called

It is easy. The program works perfectly. There is no bug.

One day, the performance problem occurs and we feel that is strange.

Now look closely at the ArrayList. We can see that it is an array only.
Then if we need more elements, the copy of array is created then the current elements are copied to new array. That is called expanded or resized.
If we want to search, the algorithm finds from first element to the last element and compare one by one.
Imagine that there is 1000 or 10000 or more books, it is very slow.

The session 1 gave two problems to think and solve:

  • What is code behind of the class and where we find it.
  • Do we need to choose the another class to store our data (books, orders, customers) and what is the priorities to choose them.

The problems will be soved in session 2....

Thank you for reading - Keep study hard

(*) Cover photo is retrieved from https://www.raconteur.net/wp-content/uploads/2021/03/transform-fast.jpg

Discover and read more posts from An Phung Duy
get started