/* ZZBench.java * * Copyright (c) 2001, Ted Nelson and Tuomas Lukka * * You may use and distribute under the terms of either the GNU Lesser * General Public License, either version 2 of the license or, * at your choice, any later version. Alternatively, you may use and * distribute under the terms of the XPL. * * See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of * the licenses. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README * file for more details. * */ /* * Written by Tuomas Lukka */ package org.gzigzag.benchmark; import org.gzigzag.*; import java.util.*; /** Test speeds of various simple java operations, * provide a generic simple speed-test framework. */ public class ZZBench { public static final String rcsid = "$Id: ZZBench.java,v 1.1 2002/10/07 12:37:33 tuukkah Exp $"; static private void p(String s) { System.out.println(s); } public void runTest(String s, Runnable r) { // Start it up so that the JIT is ready for(int i=0; i<32; i++) r.run(); // run more and more until we get over 3 secs of time. int nt = 20; long diff = 0; do { long t0 = System.currentTimeMillis(); for(int i = nt; i >= 0; i--) r.run(); long t1 = System.currentTimeMillis(); nt *= 2; diff = t1 - t0; } while(diff < 1500); p(s+": "+nt+" iter, "+diff+" ms, per iter: "+ (long)(1000000 * diff / (float)nt) + " ns"); } /** Run the given tests, time them and print out the results. * Each test should be relatively fast to execute. * @param tests Triplets of String, String, Runnable: * first string is a unique ID for the test, * second string a human-readable explanation (without newlines) * and the Runnable is the test itself. */ public void run(Object[] tests) { for(int i=0; i