import java.util.Random ; import java.util.Arrays ; import java.lang.reflect.InvocationTargetException ; public class PatriciaTest { private static final long SEED = 345859890853L ; private static final int N = 100 ; private static final int M = 100 ; public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, InterruptedException { // get params @SuppressWarnings("unchecked") Class mClass = (Class)Class.forName(args[0]) ; int rows = Integer.parseInt(args[1]) ; int cols = Integer.parseInt(args[2]) ; int expectedHash = Integer.parseInt(args[3]) ; // set up objects Matrix m = mClass.getConstructor(int.class, int.class).newInstance(rows, cols) ; Random rnd = new Random(SEED) ; for (int i = 0 ; i < rows ; ++i) { for (int j = 0 ; j < cols ; ++j) { m.set(i, j, rnd.nextDouble()) ; } } double[] v = new double[rows] ; for (int i = 0 ; i < rows ; ++i) { v[i] = rnd.nextDouble() ; } double[] w = new double[rows] ; double[] tmp = new double[cols] ; // warm up test(m, v, tmp, w, expectedHash) ; float[] times = new float[N] ; System.gc() ; Thread.sleep(100) ; // rock and roll! for (int i = 0 ; i < N ; ++i) { times[i] = test(m, v, tmp, w, expectedHash) ; } // compute stats Arrays.sort(times) ; // matrix min medin 95% max System.out.println(m.getClass().getName() + "\t" + times[0] + "\t" + times[N / 2] + "\t" + times[(int)(0.95f * N)] + "\t" + times[N - 1]) ; } private static float test(Matrix m, double[] v, double[] tmp, double[] w, int expectedHash) throws InterruptedException { long dt = 0 ; for (int i = 0 ; i < M ; ++i) { long t0 = System.nanoTime() ; m.patricia(v, tmp, w) ; dt = dt + (System.nanoTime() - t0) ; int hash = Matrix.hashCode(w) ; assert (hash == expectedHash): "bad hash: " + hash ; } return (float)dt / M ; } }