• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Optimizing Loop

November 3, 2011 By j2eereference Leave a Comment

We use loop for repeating a piece of code multiple times. So optimizing anything inside the loop body will have a good impact in overall performance.

Let’s see some of the good practices while dealing with the loop in java:

1. Method calls are very costly .Hence avoid calling methods from loop body

2. When you want to copy one array content to another, Instead of doing in the loop use System.arraycopy()

Example :

Below code is for copying contents of arrayA to arrayB

System.arraycopy(arrayA, 0, arrayB, 0, arrayA.length);

It is always better than,

for(int i = 0 ; i < arrayA.length; i ++)

{
arrayB[i] = arrayA[i];
}

3. Use int as index variable instead of other number data types

for(int i = 0 ; i < 1000; i ++);

It is always better than

for(long i = 0 ; i < 1000; i ++);

4. Avoid using method calls to find the loop termination condition

Example :

You have an arraylist and you want to do some operation as many times as the arrayList size

int size = al.size();
for(int i = 0 ; i < size ; i ++)

{
//Loop body
}

It is always better than

for(int i = 0; i < al.size(); i ++)

{
// Loop body
}

5. Try to avoid accessing array elements n loop .It is efficient to access variables.

6. Avoid Exception handling in loop

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.