skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, September 19, 2012

To improve performance by using inbuilt arrayCopy() method

Posted by Admin at 10:03 AM – 0 comments
 

This is a reusable code written in Java. User can just use arrayCopy() from System class to improve the performance while copying array into another array, where, arrayCopy is a inbuilt method in System class.


public class copyingArray {

 /**
  * 
  */
 public copyingArray() {
  super();
  // TODO Auto-generated constructor stub
 }

 public static void main(String[] args) {
  int[][] s1=new int[][] {{1,2},{3,4,5},{6,7,8,9,10}};
  int[][] s2=new int[s1.length][];
  long start = System.nanoTime();
  for(int i=0;i<s1.length;++i)
   s2[i]=s1[i];
  long end = System.nanoTime();
  System.out.println("arrayCopy : "+(end-start));
  
  long start1 = System.nanoTime();
  System.arraycopy(s1,0,s2,0,s1.length);
  long end1 = System.nanoTime();
  System.out.println("For Loop : "+(end1-start1));
  
  long start2 = System.nanoTime();
  for ( int idx = 0 ; idx < s1.length; ++idx ) {
   s2 = (int[][])s1.clone();
  }
  long end2 = System.nanoTime();
  System.out.println("Clone : "+(end2-start2));
}


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger