Advertisement
Guest User

Casting Array vs Normal Array

a guest
Sep 8th, 2012
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class JustATest {
  4.    
  5.     private class None {
  6.         int someNumber;
  7.         None (int someNumber) {
  8.             this.someNumber=someNumber;
  9.         }
  10.         int get() {
  11.             return someNumber;
  12.         }
  13.         None modify() {
  14.             //here normally happens something
  15.             return this;
  16.         }
  17.     }
  18.    
  19.     int n;
  20.     int sqrtn;
  21.     Random R = new Random();
  22.     None[] normalArray;
  23.     Object[] castingArray;
  24.    
  25.     JustATest(int n) {
  26.         this.n=n;
  27.         this.sqrtn=(int)Math.sqrt(n);
  28.         normalArray=new None[n];
  29.         castingArray=new None[n];
  30.         for (int i=0; i<n; i++) {
  31.             None N = new None(R.nextInt(1000));
  32.             normalArray[i]=N;
  33.             castingArray[i]=N;
  34.         }
  35.     }
  36.    
  37.     public int test1(int j) {
  38.         int counter=0;
  39.         None backup = (None)castingArray[j];
  40.         for (int i=n-1; i>=j; i--) castingArray[i]=((None)castingArray[i-j]).modify();
  41.         for (int i=j-1; i>0; i--) castingArray[i]=((None)castingArray[n+i-j]).modify();
  42.         castingArray[0]=backup.modify();
  43.         for (int i=0; i<sqrtn; i++) {
  44.             if (((None)castingArray[i]).get()==((None)castingArray[i*i]).get()) counter++;
  45.         }
  46.         return counter;
  47.     }
  48.    
  49.     public int test2(int j) {
  50.         int counter=0;
  51.         None backup = normalArray[j];
  52.         for (int i=n-1; i>=j; i--) normalArray[i]=normalArray[i-j];
  53.         for (int i=j-1; i>0; i--) normalArray[i]=normalArray[n+i-j];
  54.         normalArray[0]=backup;
  55.         for (int i=0; i<sqrtn; i++) {
  56.             if (normalArray[i].get()==normalArray[i*i].get()) counter++;
  57.         }
  58.         return counter;
  59.     }
  60.    
  61.    
  62.     public static void main(String[] args) {
  63.         int n = 30000;
  64.         JustATest J = new JustATest(n);
  65.         long totalCounter1=0;
  66.         long totalCounter2=0;
  67.         long a = System.currentTimeMillis();
  68.         for (int i=0; i<n; i++) totalCounter1+=J.test1(i);
  69.         long b = System.currentTimeMillis();
  70.         for (int i=0; i<n; i++) totalCounter2+=J.test2(i);
  71.         long c = System.currentTimeMillis();
  72.         System.out.println("Normal array: "+(c-b)+" ms");
  73.         System.out.println("With casting: "+(b-a)+" ms");
  74.         System.out.println("Desired result: "+totalCounter1+" = "+totalCounter2+".");
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement