Frage

I am trying to predict the estimated completion time of a simulation. I take startTime at the start of the simulation. At the end of each cycle, I take timeNow. The time elapsed timeLapsed is calculated by subtracting these two values. The average cycle time (varies per cycle) is calculated by dividing the elapsed time by the cycle number at that time, i.e. number of cycles run until then. Then I calculate the estimated completion time estimEndTime by adding the number of cycles still to go multiplied by the average cycle time to timeNow.

I think something goes wrong in the data conversion, as the estimEndTime calculation is incorrect. Its prediction is way, way too short / soon. The average cycle time avgCycleTime is calculated at around 30-50 seconds which looks correct. Trial nr of cycles is 20.

I get one warning for the conversion of the cycle number (int i) from int64 to long with possible loss of data, but since the avgCycleTime seems ok, this does not seem to be the cause for the error.

Why doesn't this work?

Code essentials:

long avgCycleTime;

DateTime startTime = DateTime::Now;

f1->textBox9->Text = startTime.ToString("dd/MM/yy  HH:mm:ss");
f1->textBox9->Update();

i = 0;  // cycle counter
while (i < nCycl)
{

    // this is where the simulation occurs

    i++;

    DateTime timeNow = DateTime::Now;
    TimeSpan timeLapsed = timeNow.Subtract(startTime);
    avgCycleTime = (timeLapsed.Ticks / i);
    DateTime estimEndTime = timeNow.AddTicks(avgCycleTime * (nCycl-i));

    f1->textBox10->Text = Convert::ToString(avgCycleTime / 10000000); // cycle time in milliseconds
    f1->textBox11->Text = estimEndTime.ToString("dd/MM/yy  HH:mm:ss");
    f1->Refresh();
}
War es hilfreich?

Lösung

The problem is that you declared avgCycleTime as long - effectively Int32. Let's assume that one cycle takes 50 seconds. In ticks it would be 50 * 10,000,000 = 500,000,000 - well fit in Int32. But then you calculate avgCycleTime * (nCycl - i) and it overflows (result will be Int32), thus you get invalid estimEndTime. So you have to declare avgCycleTime as long long or Int64.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top