Fork me on GitHub

Tutorial

Screencast

A video describing the following steps is available at Youtube.

Step by Step Tutorial

Three steps to bench your code in Eclipse:

  • Install Perfidix with Eclipse

Download latest release of Perfidix and the latest release of Perclipse (Perfidix plugin).

  • Modify your code

Annotate your code you want to see benched with the Perfidix annotations. A small example can be seen below:

import static org.junit.Assert.assertEquals;

import java.util.Random; import java.util.Stack;

import org.junit.BeforeClass;  
import org.junit.Test;  
import org.perfidix.annotation.BeforeBenchClass;
import org.perfidix.annotation.BenchClass;

@BenchClass(runs = 100)
public class FastIntStackTest {

    private static int[] data = new int[15];

    @BeforeBenchClass
    @BeforeClass
    public static void beforeClass() {
        for (int i = 0; i < data.length; i++) {
           data[i] = new Random().nextInt();
        }
    }

    @Test
    public void myStackTest() {
        final FastIntStack myStack = new FastIntStack();
        for (int i = 0; i < data.length; i++) {
            myStack.push(data[i]);
        }
        for (int i = data.length - 1; i > 0; i--) {
            assertEquals(data[i], myStack.pop());
        }
    }

    @Test
    public void normalStackTest() {
        final Stack<Integer> normalStack = new Stack<Integer>();
        for (int i = 0; i < data.length; i++) {
            normalStack.push(data[i]);
        }
        for (int i = data.length - 1; i > 0; i--) {
            assertEquals(data[i], normalStack.pop().intValue());
        }
    }

    final class FastIntStack {

        private int[] stack;

        private int size;

        FastIntStack() {
            stack = new int[32];
            size = 0;
        }

        final void push(final int element) {
            if (stack.length == size) {
                int[] biggerStack = new int[stack.length << 1];
                System.arraycopy(stack, 0, biggerStack, 0, stack.length);
                stack = biggerStack;
            }
            stack[size++] = element;
        }

        final int peek() {
            return stack[size - 1];
        }

        final int get(final int position) {
            return stack[position];
        }

        final int pop() {
            return stack[--size];
        }

        final void clear() {
            size = 0;
        }

        final int size() {
            return size;
        }

    }
}  
  • Run the bench out of Eclipse Insert the Perfidix.jar into your classpath and right-click on your project where the code to be benched is located in.