Many performance related issues become difficult to debug, if we do not know how to measure the time take to execute a piece of code. If we introduce a couple of simple lines in our code base,it can be measure with a decent accuracy.
ThreadMXBean threadMxBeam = ManagementFactory.getThreadMXBean();
long timeBefore = threadMxBeam.getCurrentThreadCpuTime();
//Code whose performance has to be tested goes here.
long timeAfter = threadMxBeam.getCurrentThreadCpuTime();
System.out.println("======Time Taken to execute this code==" +(timeAfter-timeBefore));
There is another API which gives time in milli seconds. But some developers say that it is not so effective.
long startTimeStamp = System.currentTimeMillis();
//Code whose performance has to be tested goes here.
long endTimeStamp = System.currentTimeMillis();
long timeTaken = (endTimeStamp - startTimeStamp);
When performance issues are discovered at a later stage of the project, fixing them can have a significant impact on the codebase. Whilst it may not be practical to expect our code to be very performant in the beginning itself, if we can keep a few things in our mind while coding, there is a chances to reduce such occurrences.
Some ways to improve the performance:
1. Use of database indexes optimally. It should be examined for all SQL that gets run. If you select on a table using a single field that is not indexed then an entire table scan is done which would take a very long time on larger tables. If you’re selecting using two values then at least one should be indexed as that way the DB would initially go through the index and then restrict those results using the second field. The more often an SQL is executed the more critical that it is indexed, although it should also be noted that indexes will slow down the inserts slightly.
2. Avoid unnecessary java looping. Instead, try to restrict them in the database itself before bringing them to the java layer.
3. Use sql joins instead of multiple queries. This can reduce the number of database calls.
4. Use caching for frequently read tables. For example, if a common table such as person has to be read on every page, it may be worth storing the required person's details in a cache in data access layer. If data is present in the cache, no need to query the database again. Another way is to put the details in a session. But potential security issues also have to be given due importance while considering this.
5.Avoid unnecessary lines of code. Sometimes it is possible that mistakenly, some java lines are present even though they are not necessary for every flow. Such lines should be optimized.
6. Carefully considering the && and || conditions in the if blocks. The condition which executes faster should come first. Since, success or failure of this condition will decide whether to execute the next condition or not.
7. Avoid unnecessary object instantiation. Split large methods into smaller ones wherever possible, since it is believed to improve garbage collection.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment