Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class JustATest {
- private class None {
- int someNumber;
- None (int someNumber) {
- this.someNumber=someNumber;
- }
- int get() {
- return someNumber;
- }
- None modify() {
- //here normally happens something
- return this;
- }
- }
- int n;
- int sqrtn;
- Random R = new Random();
- None[] normalArray;
- Object[] castingArray;
- JustATest(int n) {
- this.n=n;
- this.sqrtn=(int)Math.sqrt(n);
- normalArray=new None[n];
- castingArray=new None[n];
- for (int i=0; i<n; i++) {
- None N = new None(R.nextInt(1000));
- normalArray[i]=N;
- castingArray[i]=N;
- }
- }
- public int test1(int j) {
- int counter=0;
- None backup = (None)castingArray[j];
- for (int i=n-1; i>=j; i--) castingArray[i]=((None)castingArray[i-j]).modify();
- for (int i=j-1; i>0; i--) castingArray[i]=((None)castingArray[n+i-j]).modify();
- castingArray[0]=backup.modify();
- for (int i=0; i<sqrtn; i++) {
- if (((None)castingArray[i]).get()==((None)castingArray[i*i]).get()) counter++;
- }
- return counter;
- }
- public int test2(int j) {
- int counter=0;
- None backup = normalArray[j];
- for (int i=n-1; i>=j; i--) normalArray[i]=normalArray[i-j];
- for (int i=j-1; i>0; i--) normalArray[i]=normalArray[n+i-j];
- normalArray[0]=backup;
- for (int i=0; i<sqrtn; i++) {
- if (normalArray[i].get()==normalArray[i*i].get()) counter++;
- }
- return counter;
- }
- public static void main(String[] args) {
- int n = 30000;
- JustATest J = new JustATest(n);
- long totalCounter1=0;
- long totalCounter2=0;
- long a = System.currentTimeMillis();
- for (int i=0; i<n; i++) totalCounter1+=J.test1(i);
- long b = System.currentTimeMillis();
- for (int i=0; i<n; i++) totalCounter2+=J.test2(i);
- long c = System.currentTimeMillis();
- System.out.println("Normal array: "+(c-b)+" ms");
- System.out.println("With casting: "+(b-a)+" ms");
- System.out.println("Desired result: "+totalCounter1+" = "+totalCounter2+".");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement