Fxguy1
Fxguy1

Reputation: 85

Estimate time remaining with variable runtime

I have a project in Java that is scraping a fairly large amount of HTML and I'd like to display an estimate of the time remaining.

I thought I could grab the time (in milliseconds) at the start and at the end of each loop and then subtract to get the total time to run. Then I figured I know exactly how many times I need to run through so I could just multiply by that to get a total estimated time.

Then for estimated time remaining I could just keep a running total time and subtract from the total estimated time.

The problem is that my output seems to be negative numbers...?

CurrentPage ++; long timeend = System.currentTimeMillis();

            // Calculate Time
            long runtime = timestart - timeend;
            long timetotal = 0;
            timetotal = timetotal + runtime;
            long averagetime = timetotal / CurrentPage;
            long timeestimated = averagetime * totalpages;
            long timeremaining = timeestimated -timetotal;
            
            
            String RunTime= String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(runtime), TimeUnit.MILLISECONDS.toSeconds(runtime));
            String AverageTime=String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(averagetime), TimeUnit.MILLISECONDS.toSeconds(averagetime));
            String TimeEstimated=String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(timeestimated), TimeUnit.MILLISECONDS.toSeconds(timeestimated));
            String TimeRemaining=String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(timeremaining), TimeUnit.MILLISECONDS.toSeconds(timeremaining));
            
            
            System.out.println("Page " + CurrentPage +" completed");
            System.out.println("Current Runtime = " + RunTime);
            System.out.println("Average Runtime = " + AverageTime);
            System.out.println("Estimated time = " + TimeEstimated);
            System.out.println("Estimated time remaining  = " +TimeRemaining);

My output looks like this:

Page 9 completed
Current Runtime = 0 min, -3 sec
Average Runtime = 0 min, 0 sec
Estimated time = 0 min, -3 sec
Estimated time remaining  = 0 min, 0 sec
Page 10 completed
Current Runtime = 0 min, -3 sec
Average Runtime = 0 min, 0 sec
Estimated time = 0 min, -3 sec
Estimated time remaining  = 0 min, 0 sec
Page 11 completed
Current Runtime = 0 min, -4 sec
Average Runtime = 0 min, 0 sec
Estimated time = 0 min, -3 sec
Estimated time remaining  = 0 min, 0 sec

So I'm obviously doing something wrong, but cant quite figure out what it is. When I run through 10 loops the Current Runtime adds up to 20 seconds and the build /runtime is 27 seconds... so I'm at a loss.

Upvotes: 0

Views: 215

Answers (0)

Related Questions