0

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.

Fxguy1
  • 85
  • 6
  • 2
    I think runtime should be defined as `long runtime = timeEnd - timeStart;`. The end time will be greater than the start time, so with your way that will give negative numbers. – Carcigenicate Feb 17 '19 at 18:28
  • Yes! Thank you! I'm horrible with math....That fixed the negative value but the estimates / total time still seem to be off quite a bit (as in I ran through 10 loops and the calculated was 3 sec and the build was 27 seconds.... – Fxguy1 Feb 17 '19 at 18:37
  • 1
    It just stood out to me since I've written that exact equation like a hundred times. Note though, you could have debugged this fairly easily. If you would have checked the value of the variables that lead to the end result, you would have noticed that `runtime` was negative. Make sure you're thoroughly checking your data before consulting for help. Debugging is a crucial skill. – Carcigenicate Feb 17 '19 at 18:39

0 Answers0