calculate elapsed seconds

This is a discussion on calculate elapsed seconds within the C# Programming forums, part of the General Programming Boards category; Hello everyone, What is the most convenient way to check whether elapsed time from some time point to another time ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    calculate elapsed seconds

    Hello everyone,


    What is the most convenient way to check whether elapsed time from some time point to another time point is larger than 2 seconds?

    Currently, I am using TimeSpan, and I am using stupid method to check hour/minute/second one by one to decide. Are there any quick way to check?


    thanks in advance,
    George

  2. #2
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    timer. Check this:

    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main () {
        time_t start, end;
        double length;
    
        timer(&start);
        printf ("Start counting and press any key.");
        getchar ();
        timer(&end);
        length = difftime(end, start);
        printf ("%.0f seconds.", length);
    }
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm using a System.DateTime which has been sufficient for my needs so far.
    Code:
    System.DateTime TimePoint1 = System.DateTime.Now;
    
    //Do some timeconsuming stuff here
    
    System.DateTime TimePoint2 = System.DateTime.Now;
    
    long MillisecondsBetweenTimePoints = (TimePoint2.Ticks - TimePoint1.Ticks) / 10000;
    if(MillisecondsBetweenTimePoints > 2000)
    {
      //Do some stuff here if the timeconsuming stuff took more than 2 seconds
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    It is not C# code, guesst. :-)


    Quote Originally Posted by guesst View Post
    timer. Check this:

    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main () {
        time_t start, end;
        double length;
    
        timer(&start);
        printf ("Start counting and press any key.");
        getchar ();
        timer(&end);
        length = difftime(end, start);
        printf ("%.0f seconds.", length);
    }

    Cool, Magos!


    Quote Originally Posted by Magos View Post
    I'm using a System.DateTime which has been sufficient for my needs so far.
    Code:
    System.DateTime TimePoint1 = System.DateTime.Now;
    
    //Do some timeconsuming stuff here
    
    System.DateTime TimePoint2 = System.DateTime.Now;
    
    long MillisecondsBetweenTimePoints = (TimePoint2.Ticks - TimePoint1.Ticks) / 10000;
    if(MillisecondsBetweenTimePoints > 2000)
    {
      //Do some stuff here if the timeconsuming stuff took more than 2 seconds
    }

    regards,
    George

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    The most convenient way i found was using System.Diagnostics.Stopwatch.

    (In fact, i was just writing a stopwatch class when i figured out there was already one)


    Code:
    using System.Diagnostics;
    
    Stopwatch sw = new Stopwatch();
    sw.Start();
    
    
    while(sw.ElapsedMilliseconds < 2000)
    {
    
        if(sw.ElapsedMilliseconds > 1000) { }
    }
    signature under construction

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Raven,


    What are the pros and cons comparing DateTime/TimeSpan with Stopwatch?

    Quote Originally Posted by Raven Arkadon View Post
    The most convenient way i found was using System.Diagnostics.Stopwatch.

    (In fact, i was just writing a stopwatch class when i figured out there was already one)


    Code:
    using System.Diagnostics;
    
    Stopwatch sw = new Stopwatch();
    sw.Start();
    
    
    while(sw.ElapsedMilliseconds < 2000)
    {
    
        if(sw.ElapsedMilliseconds > 1000) { }
    }

    regards,
    George

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Stopwatch has 2 static properties:
    .) IsHighResolution and
    .) Frequency - the number of ticks - so even if you calculate in ticks you shouldn't assume that its constant (e.g. just divide by 10000), but optain that value from from some api function (note that these ticks may only be valid for the stopwatch object, since other objects might not use the high resolution timer).
    (Unfortunately i don't know if high resolution timers have a negative performance inpact)

    For example, in my case IsHighResolution is true, so the Frequency the stopwatch is running is 3192270000 ticks/sec. (woot, just figured that out, thats high)


    Also, the difference between two DateTimes is a TimeSpan (sometimes things are quite logic
    So DateTime is a point in time, and TimeSpan is the difference between 2 timestamps.

    You could also write:
    Code:
    DateTime start = DateTime.Now;
    
    // some code;
    
    DateTime end = DateTime.Now;
    
    // Note that DateTime has an overloaded - operator which returns a TimeSpan:
    TimeSpan elapsed = end - start;
    Btw, concerning your initial question:
    Why don't you use the .TotalXXX methods of the TimeSpan? Actually you could have figured that out with IntelliSense
    E.g. elapsed.TotalSeconds > 2.0, or elapsed.TotalMilliseconds > 2000.0
    (note: the TotalXXX methods return the result as double, to avoid overflows)

    Oh the advantage of stopwatch: Better timing resolution, and "more object oriented" usage - to the 1/10 of a millisecond (the other method only rounds to the nearest 10 ms)
    Last edited by Raven Arkadon; 04-20-2008 at 04:49 AM.
    signature under construction

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Raven,


    I have two more comments,

    1.

    About IsHighResolution, it is readonly, who is responsible to set its value?

    http://msdn2.microsoft.com/en-us/lib...esolution.aspx

    2.

    Do you mean the frequency value for different H/W may be different even in the same .NET version?

    Quote Originally Posted by Raven Arkadon View Post
    Stopwatch has 2 static properties:
    .) IsHighResolution and
    .) Frequency - the number of ticks - so even if you calculate in ticks you shouldn't assume that its constant (e.g. just divide by 10000), but optain that value from from some api function (note that these ticks may only be valid for the stopwatch object, since other objects might not use the high resolution timer).
    (Unfortunately i don't know if high resolution timers have a negative performance inpact)

    For example, in my case IsHighResolution is true, so the Frequency the stopwatch is running is 3192270000 ticks/sec. (woot, just figured that out, thats high)


    Also, the difference between two DateTimes is a TimeSpan (sometimes things are quite logic
    So DateTime is a point in time, and TimeSpan is the difference between 2 timestamps.

    You could also write:
    Code:
    DateTime start = DateTime.Now;
    
    // some code;
    
    DateTime end = DateTime.Now;
    
    // Note that DateTime has an overloaded - operator which returns a TimeSpan:
    TimeSpan elapsed = end - start;
    Btw, concerning your initial question:
    Why don't you use the .TotalXXX methods of the TimeSpan? Actually you could have figured that out with IntelliSense
    E.g. elapsed.TotalSeconds > 2.0, or elapsed.TotalMilliseconds > 2000.0
    (note: the TotalXXX methods return the result as double, to avoid overflows)

    Oh the advantage of stopwatch: Better timing resolution, and "more object oriented" usage - to the 1/10 of a millisecond (the other method only rounds to the nearest 10 ms)

    regards,
    George

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    About IsHighResolution, it is readonly, who is responsible to set its value?
    The system. (Well actually the Stopwatch queries the system for a high resolution timer - and if it doesn't find one, i guess it resorts to the DateTime method).

    Do you mean the frequency value for different H/W may be different even in the same .NET version?
    No, not for DateTime (The stopwatch frequence may vary though), but its better style to use constants provided api (if there are any)
    E.g. In C there is the constant CLOCKS_PER_SEC which is usually 1000000, but there might be exotic systems where it has a different value.
    Last edited by Raven Arkadon; 04-20-2008 at 07:17 AM.
    signature under construction

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Raven,


    Your reply is clear. What do you think Stopwatch is "more object oriented" usage? Compared with what do you have this conclusion? :-)

    Quote Originally Posted by Raven Arkadon View Post
    The system. (Well actually the Stopwatch queries the system for a high resolution timer - and if it doesn't find one, i guess it resorts to the DateTime method).



    No, not for DateTime (The stopwatch frequence may vary though), but its better style to use constants provided api (if there are any)
    E.g. In C there is the constant CLOCKS_PER_SEC which is usually 1000000, but there might be exotic systems where it has a different value.

    regards,
    George

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    >> What do you think Stopwatch is "more object oriented" usage? Compared with what do you have this conclusion? :-)

    By "more object oriented" i mean that the measuring is packed into a class. You just call Start(), Elapsed(Milli)Seconds and End() which seems more natural than defining a variable start, and then retrieve the current time in substract start from it again.
    signature under construction

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Raven,


    Got your ponits.

    Quote Originally Posted by Raven Arkadon View Post
    >> What do you think Stopwatch is "more object oriented" usage? Compared with what do you have this conclusion? :-)

    By "more object oriented" i mean that the measuring is packed into a class. You just call Start(), Elapsed(Milli)Seconds and End() which seems more natural than defining a variable start, and then retrieve the current time in substract start from it again.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. How to build a timer that counts the seconds elapsed?
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-28-2007, 12:26 PM
  3. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM
  4. Algo for create hours and minutes with given seconds
    By BianConiglio in forum C Programming
    Replies: 6
    Last Post: 05-30-2004, 06:50 PM
  5. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 01:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21