[Webinar] Streamline your web hosting managementRegister Today


Predict programme finish time

Posted on 2011-02-14
Medium Priority
Last Modified: 2012-08-13
Hi *,

I have some programs which run for a long time; sometimes they run for days/weeks. I can calculate how far through a programme is in percent. What I would like to do is predict (roughly, assuming the programme speed is linear) when a programme should finish.

I have thought that this should be possible by recording the date and time when the programme starts, and then using the current percentage to predict forwards. What I would like is to be able to display a string, containing an estimated time left; perhaps like this:
w0 d13 h17 m22

One thing is that, I currently (due to computer setup) I have another programme displaying the current progress of a programme. Therefore, I have a programme (which I want to monitor and predict finish time for) saving the current progress to a file. This file is then read by the other programme and displays the progress to the user. Therefore, the start time would need to be saved to this same text file and read by the other programme.

Any suggestions? Thoughts?

Question by:James_h1023
  • 4
  • 3
LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 34888027
If you have the start time, and the percentage of progress, then you can calculate the end time :

        end_time = start_time + ((current_time - start_time) / percentage)

where all times are in seconds since the epoch (or something similar), and percentage is a value between 0 and 1.

Author Comment

ID: 34888189
Thanks for your response.

This is true, I had completely forgotton that one can get the seconds since the epoch in Unix.

Very simply question!

LVL 53

Expert Comment

ID: 34888381
Btw, be careful for division by 0 ;)
The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.


Author Comment

ID: 34888462
In fact, I say it was simple, but I am having a nightmare implementing it!

I am getting all stuck up in variables types.

I have the attached.

I'm looking at the lines with lots of 'X's. I think I currently have problems with casting between floats and ints, as end up with -2010200302 or something similar for predTime.

Also, how could I convert from int (assuming it is calcaulted properly, i.e., predTime) back to time_t which ctime() needs so I can output the time as a nice string.

Sorry, I close the question too early. Nevermind, if it is too late.

// Get the current progress
float progress=0.23;

// Get the start time in seconds
char *progressC[100];
int  progressD=0;
FILE *f1;
f1 = fopen("progress.dat", "rt");
fgets(progressC, 100, f1);
progressD = atoi(progressC);

// Get the current time in seconds
time_t tim=time(NULL);

// Predict end time in seconds
printf("%s\n", predTimeC);

Open in new window

LVL 53

Expert Comment

ID: 34888784
>> Sorry, I close the question too early. Nevermind, if it is too late.

It's certainly not too late. As long as it has to do with the original question (which is definitely the case here), I'm always ready to provide additional support.

>> char *progressC[100];

You probably want :

        char progressC[100] = "";

(similarly for predTimeC further in the code)

>> progressD = atoi(progressC);

Representating the time in seconds since the epoch, can be a large value that might not fit in a (signed) int.
It's probably best to use a time_t, and to store it in binary form in the file.

predTime could then be calculated something like this (assuming that time_t is an integer value that represents the number of seconds since the epoch on your specific platform - which is the case for the large majority of platforms these days) :

        time_t predTime = progressD + ((tim - progressD) / progress);

(I would pick more meaningful/consistent variable names though - see my first post eg.)

Note that this will NOT work on platforms where time_t is NOT an integer value that represents the number of seconds since the epoch on your specific platform. But as said : that is quite unlikely.
LVL 53

Expert Comment

ID: 34888794
Oh, and you can format a time_t as a string using strftime :


(see the code example at the bottom of that reference page)

Author Comment

ID: 34889959
Wonderful, that's got my silly questions answered.

Many thanks,

Featured Post

KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question