Advertisement
Guest User

Untitled

a guest
Dec 7th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. interface Function<T> {
  6.     long perform(T parameter, long x);
  7. }
  8.  
  9. class MyArray<T> {
  10.  
  11.     T[] array;
  12.     long x;
  13.  
  14.     public MyArray(int size, Class<T> type, long x) {
  15.         array = (T[]) Array.newInstance(type, size);
  16.         this.x = x;
  17.     }
  18.  
  19.     public void forEach(Function<T> function) {
  20.         for (T element : array) {
  21.             x = function.perform(element, x);
  22.         }
  23.     }
  24. }
  25.  
  26. class Compute {
  27.     int factor;
  28.     final long constant;
  29.  
  30.     public Compute(int factor, long constant) {
  31.         this.factor = factor;
  32.         this.constant = constant;
  33.     }
  34.  
  35.     public long compute(long parameter, long x) {
  36.         return x * factor + parameter + constant;
  37.     }
  38. }
  39.  
  40. public class Main {
  41.  
  42.     public static void main(String[] args) {
  43.         List<Long> numbers = new ArrayList<Long>(50000000);
  44.         for (int i = 0; i < 50000000; i++) {
  45.             numbers.add(i * i + 5L);
  46.         }
  47.  
  48.         long x = 234553523525L;
  49.  
  50.         long time = System.currentTimeMillis();
  51.         for (int i = 0; i < numbers.size(); i++) {
  52.             x += x * 7 + numbers.get(i) + 3;
  53.         }
  54.         System.out.println(System.currentTimeMillis() - time);
  55.         System.out.println(x);
  56.         x = 0;
  57.         time = System.currentTimeMillis();
  58.         for (long i : numbers) {
  59.             x += x * 7 + i + 3;
  60.         }
  61.         System.out.println(System.currentTimeMillis() - time);
  62.         System.out.println(x);
  63.         x = 0;
  64.         numbers = null;
  65.         MyArray<Long> myArray = new MyArray<Long>(50000000, Long.class, 234553523525L);
  66.         for (int i = 0; i < 50000000; i++) {
  67.             myArray.array[i] = i * i + 3L;
  68.         }
  69.         time = System.currentTimeMillis();
  70.         myArray.forEach(new Function<Long>() {
  71.  
  72.             public long perform(Long parameter, long x) {
  73.                 return x * 8 + parameter + 5L;
  74.             }
  75.         });
  76.         System.out.println(System.currentTimeMillis() - time);
  77.         System.out.println(myArray.x);
  78.         myArray = null;
  79.         myArray = new MyArray<Long>(50000000, Long.class, 234553523525L);
  80.         for (int i = 0; i < 50000000; i++) {
  81.             myArray.array[i] = i * i + 3L;
  82.         }
  83.         time = System.currentTimeMillis();
  84.         myArray.forEach(new Function<Long>() {
  85.  
  86.             public long perform(Long parameter, long x) {
  87.                 return new Compute(8, 5).compute(parameter, x);
  88.             }
  89.         });
  90.         System.out.println(System.currentTimeMillis() - time);
  91.         System.out.println(myArray.x);
  92.     }
  93. }
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement