java queue

import java.util.ArrayDeque;

public class Test {
	static final int size = (1 << 22) - 1;

	public static final void main(String args[]) {
		ArrayDeque<Integer> q1 = new ArrayDeque<Integer>();
		IntQueue q2 = new IntQueue();
		long start, end;
		
		start = System.nanoTime();
		for (int i = 0; i < size; i++) {
			q1.add(i);
		}
		for (int i = 0; i < size; i++) {
			if (!q1.isEmpty()) {
				q1.poll();
			}
		}
		end = System.nanoTime();
		System.out.println(end - start);

		start = System.nanoTime();
		for (int i = 0; i < size; i++) {
			q1.add(i);
		}
		for (int i = 0; i < size; i++) {
			if (!q1.isEmpty()) {
				q1.poll();
			}
		}
		end = System.nanoTime();
		System.out.println(end - start);

		start = System.nanoTime();
		for (int i = 0; i < size; i++) {
			q2.add(i);
		}
		for (int i = 0; i < size; i++) {
			if (q2.notEmpty()) {
				q2.poll();
			}
		}
		end = System.nanoTime();
		System.out.println(end - start);
	}

	static class IntQueue {
		private static final int size = 1 << 22;
		private static final int mask = size - 1;
		private final int buf[] = new int[size];
		private int start = 0, last = 0;

		public void add(int x) {
			buf[last] = x;
			last = (last + 1) & mask;
		}

		public int peek() {
			return buf[start];
		}

		public int poll() {
			int res = buf[start];
			start = (start + 1) & mask;
			return res;
		}

		public boolean notEmpty() {
			return start != last;
		}
	}
}
1126910466
958816411
15457662

それなりにIntQueueは速いけど、機能を考えるとArrayDequeで作って、遅い部分がQueueであればIntQueueみたいのつかってみるのもありかもしれない。