commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles Sadowski <gil...@harfang.homelinux.org>
Subject [Math] Simple benchmarking utility
Date Sun, 24 Jul 2011 22:09:09 GMT
Hello.

Finding myself repeatedly writing the same few lines when trying to figure
out which of several implementations of some functionality was running
faster, I wonder wether it would be interesting to add a little utility in
the "test" section of the source tree. Something like the following:
---CUT---
    /**                                                                                  
                                                     
     * Timing.                                                                           
                                                     
     *                                                                                   
                                                     
     * @param repeatChunk Each timing measurement will done done for that                
                                                     
     * number of repeats of the code.                                                    
                                                     
     * @param repeatStat Timing will be averaged over that number of runs.               
                                                     
     * @param methods Code being timed.                                                  
                                                     
     * @return for each of the given {@code methods}, the averaged time (in              
                                                     
     * milliseconds) taken by a call to {@code run}.                                     
                                                     
     */
    public static double[] time(int repeatChunk,
                                int repeatStat,
                                Runnable ... methods) {
        final int numMethods = methods.length;
        final double[][] times = new double[numMethods][repeatStat];

        long time;
        for (int k = 0; k < repeatStat; k++) {
            for (int j = 0; j < numMethods; j++) {
                Runnable r = methods[j];
                time = System.nanoTime();
                for (int i = 0; i < repeatChunk; i++) {
                    r.run();
                }
                times[j][k] = (System.nanoTime() - time) * NANO_TO_MILLI;
            }
        }

        final MultivariateRealFunction acc = FunctionUtils.collector(new Add(), 0);
        final double[] avgTimes = new double[numMethods];

        final double normFactor = 1d / (repeatStat * repeatChunk);
        for (int j = 0; j < numMethods; j++) {
            avgTimes[j] = normFactor * acc.value(times[j]);
        }

        return avgTimes;
    }
---CUT---

The idea is to have "interleaved" calls to the candidate implementations, so
that (hopefully) they will be penalized (or benefit) in the same way by what
the JVM is doing (GC or JIT compilation or ...) while the benchmark is
running.

Does this make sense?


Regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message