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-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to