O'Reilly logo

Jakarta Commons Cookbook by Timothy M. O'Brien

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.10. Estimating the Amount of Time Left in a Process

Problem

You are running a program that takes a long time to execute, and you need to present the user with an estimated time until completion.

Solution

Use Commons Math’s SimpleRegression and Commons Lang’s StopWatch to create a ProcessEstimator class that can be used to predict when a particular program will be finished. Your program needs to process a number of records, and this program could take a few hours to finish. You would like to provide some feedback, and, if you are confident that each record will take roughly the same amount of time, you can use SimpleRegression’s slope and intercept to estimate the time when all records will be processed. Example 8-1 defines the ProcessEstimator class that combines the power of StopWatch and ProcessEstimator to estimate the time remaining in a process.

Example 8-1. ProcessEstimator to estimate time of program execution

package com.discursive.jccook.math.timeestimate; import org.apache.commons.lang.time.StopWatch; import org.apache.commons.math.stat.multivariate.SimpleRegression; public class ProcessEstimator { private SimpleRegression regression = new SimpleRegression( ); private StopWatch stopWatch = new StopWatch( ); // Total number of units private int units = 0; // Number of units completed private int completed = 0; // Sample rate for regression private int sampleRate = 1; public ProcessEstimator( int numUnits, int sampleRate ) { this.units = numUnits; this.sampleRate = sampleRate; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required