Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.List;
- interface Function<T> {
- long perform(T parameter, long x);
- }
- class MyArray<T> {
- T[] array;
- long x;
- public MyArray(int size, Class<T> type, long x) {
- array = (T[]) Array.newInstance(type, size);
- this.x = x;
- }
- public void forEach(Function<T> function) {
- for (T element : array) {
- x = function.perform(element, x);
- }
- }
- }
- class Compute {
- int factor;
- final long constant;
- public Compute(int factor, long constant) {
- this.factor = factor;
- this.constant = constant;
- }
- public long compute(long parameter, long x) {
- return x * factor + parameter + constant;
- }
- }
- public class Main {
- public static void main(String[] args) {
- List<Long> numbers = new ArrayList<Long>(50000000);
- for (int i = 0; i < 50000000; i++) {
- numbers.add(i * i + 5L);
- }
- long x = 234553523525L;
- long time = System.currentTimeMillis();
- for (int i = 0; i < numbers.size(); i++) {
- x += x * 7 + numbers.get(i) + 3;
- }
- System.out.println(System.currentTimeMillis() - time);
- System.out.println(x);
- x = 0;
- time = System.currentTimeMillis();
- for (long i : numbers) {
- x += x * 7 + i + 3;
- }
- System.out.println(System.currentTimeMillis() - time);
- System.out.println(x);
- x = 0;
- numbers = null;
- MyArray<Long> myArray = new MyArray<Long>(50000000, Long.class, 234553523525L);
- for (int i = 0; i < 50000000; i++) {
- myArray.array[i] = i * i + 3L;
- }
- time = System.currentTimeMillis();
- myArray.forEach(new Function<Long>() {
- public long perform(Long parameter, long x) {
- return x * 8 + parameter + 5L;
- }
- });
- System.out.println(System.currentTimeMillis() - time);
- System.out.println(myArray.x);
- myArray = null;
- myArray = new MyArray<Long>(50000000, Long.class, 234553523525L);
- for (int i = 0; i < 50000000; i++) {
- myArray.array[i] = i * i + 3L;
- }
- time = System.currentTimeMillis();
- myArray.forEach(new Function<Long>() {
- public long perform(Long parameter, long x) {
- return new Compute(8, 5).compute(parameter, x);
- }
- });
- System.out.println(System.currentTimeMillis() - time);
- System.out.println(myArray.x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement