Solved

Calculating Time Elapsed

Posted on 2006-11-18
12
361 Views
Last Modified: 2010-04-16
Hi,

I am using C#. I have timer running on my screen

I want to estimate the time left to complete the process

Can anyone helpme with the logic

I have no of records executed after each process and max no of records available.

I wish to calculate that how much records are executed in 1 min and estimated time left in completing all the records.

Or there is some better approach, please help me?

Can anyone help me with the logic?

Thanks in Advance
0
Comment
Question by:dinesh_bali
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 17973037
Here is an example:

  static void Main()
  {
    const int n = 10;

    DateTime start = DateTime.Now;

    //Report Initial Status
    Console.WriteLine("Complete: 0/" + n + ", Time Remaining: Unknown");

    //Do Work (Part 1)
    System.Threading.Thread.Sleep(1000);

    for (int i = 1; i < n; i++)
    {
      //Report Status
      Console.WriteLine("Complete: " + i + "/" + n + ", Time Remaining: " + TimeRemaining(start, i, n));

      //Do Work (Parts 2 through n)
      System.Threading.Thread.Sleep(1000);
    }

    //Report Final Status
    Console.WriteLine("Complete: " + n + "/" + n + ", Time Remaining: 00:00:00");

    Console.ReadLine();
  }

  static TimeSpan TimeRemaining(DateTime start, int complete, int total)
  {
    if (complete < 1 || complete > total)
      throw new ArgumentException();

    long elapsed = DateTime.Now.Subtract(start).Ticks;
    double factor = (double)(total - complete) / (double)complete;

    return new TimeSpan((long)(elapsed * factor));
  }
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17973052
Actually, this is simpler!

  static void Main()
  {
    const int n = 10;

    DateTime start = DateTime.Now;

    for (int i = 0; i < n; i++)
    {
      //Do Work
      System.Threading.Thread.Sleep(1000);

      //Report Status
      Console.WriteLine("Complete: " + (i + 1) + "/" + n + ", Time Remaining: " + TimeRemaining(start, i + 1, n));
    }

    Console.ReadLine();
  }
0
 
LVL 7

Expert Comment

by:mjmarlow
ID: 17973101
What is the range of possible record counts?
What is the range of time to process a single record?
0
 

Author Comment

by:dinesh_bali
ID: 17974102
Thanks for all you are trying to help me.

I have calculated manually that my process process 400 records in a minute

but this depends on large and small records. So time estimate may vary time to time.

I have calculated time elapsed from the following method

        private void timer1_Tick(object sender, EventArgs e)
        {
            seconds++;
            TimeSpan ts = new TimeSpan(0, 0, seconds);
            TimeSpanConverter tsc = new TimeSpanConverter();
            lblTimer.Text = tsc.ConvertToString(ts);            
        }



My max records count can be 100000

and in while loop i am increasing count with count++


I will try above solutions and will come back. In the mean time, if someone has more simpler logic than please tell me the solutions

Thanking u
0
 

Author Comment

by:dinesh_bali
ID: 17974106
Hi Expert1701,

Can u please tell me why u have written this method as static

static TimeSpan TimeRemaining(DateTime start, int complete, int total)
  {
    if (complete < 1 || complete > total)
      throw new ArgumentException();

    long elapsed = DateTime.Now.Subtract(start).Ticks;
    double factor = (double)(total - complete) / (double)complete;

    return new TimeSpan((long)(elapsed * factor));
  }

cannot we write

public TimeSpan TimeRemaining(DateTime start, int complete, int total)
  {
    if (complete < 1 || complete > total)
      throw new ArgumentException();

    long elapsed = DateTime.Now.Subtract(start).Ticks;
    double factor = (double)(total - complete) / (double)complete;

    return new TimeSpan((long)(elapsed * factor));
  }

Many Thanks
0
 

Author Comment

by:dinesh_bali
ID: 17974142
Hi,

Your below solution works fine, Many thanks.

static TimeSpan TimeRemaining(DateTime start, int complete, int total)
  {
    if (complete < 1 || complete > total)
      throw new ArgumentException();

    long elapsed = DateTime.Now.Subtract(start).Ticks;
    double factor = (double)(total - complete) / (double)complete;

    return new TimeSpan((long)(elapsed * factor));
  }


But I want my estimated time left in the format 00:00:00

But it is in

00:00:00.0000000

Can you please help me in doing this

Also why u have written line
 System.Threading.Thread.Sleep(1000);

for how much time this will make my syetem sleep, 1 min or 1 sec

how much time is 1000

Hope to have your early response

Many thanks for your help
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dinesh_bali
ID: 17974146
Also, please tell me

Can u please tell me why u have written this method as static

static TimeSpan TimeRemaining(DateTime start, int complete, int total)

cannot we write

public TimeSpan TimeRemaining(DateTime start, int complete, int total)

Awaiting your response
Thanks
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 17975392
Hi dinesh_bali,

To answer your last open questions, you can leave the static keyword. Expert1701 just added it because he is calling it from a static method (main), and therefore he had to add the static keyword. So yes you can use:

public TimeSpan TimeRemaining(DateTime start, int complete, int total)

To use the format 00:00:00, you could change the code slightly:

for (int i = 1; i < n; i++)
{
      //Report Status
      string timeRemaining = TimeRemaining(start, i, n).ToString();
      timeRemaining = timeRemaining.Substring(0, timeRemaining.IndexOf("."));
      Console.WriteLine("Complete: " + i + "/" + n + ", Time Remaining: " + timeRemaining);

      //Do Work (Parts 2 through n)
      System.Threading.Thread.Sleep(1000);
}

HTH,
Razzie
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17975594
dinesh_bali, I'm sorry I missed your replies - please assign the points to Razzie_ for this question.

If I can offer one more thing, maybe a function like the following would be appropriate given your use:

  string TimeRemaining(DateTime start, int complete, int total)
  {
    if (complete < 1)
      return "Unknown";
    else if (complete >= total)
      return "00:00:00";
    else
    {
      long elapsed = DateTime.Now.Subtract(start).Ticks;
      double factor = (double)(total - complete) / (double)complete;

      TimeSpan remaining = new TimeSpan((long)(elapsed * factor));

      return String.Format("{0:00}:{1:00}:{2:00}", (int)remaining.TotalHours, remaining.Minutes, remaining.Seconds);
    }
  }

0
 

Author Comment

by:dinesh_bali
ID: 17975946
Hi,

Many thanks for your response.

But No- one has guided me that why we have used

System.Threading.Thread.Sleep(1000);

If I use this, this will make my system more slower.

Is this neccassary to use.

Please help

Many Thanks
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 17976059
Thread.Sleep(1000) is only used by Expert1701 to demonstrate what the code does. It makes the code perform slower so you can see what happen. If he would have omitted it, it would be hard for you to see what happens. So yes, you can leave the Thread.Sleep(1000) :)

And I do not agree with Expert1701 :P Though he missed your last questions, he provided you with the basic solution, so he should receive the larger amount of the points :)

HTH,
Razzie
0
 

Author Comment

by:dinesh_bali
ID: 17977781
Thanks for all your help.

Kind Regards
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now