Thread: Timing

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    241

    Timing

    I have a console app with vs.net with mfc support, and I used the CTime class to get the time after and before the operation. what code would I need to use those times to find the number of seconds that it took? (This operation may take more than one day...)

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Re: Timing

    Originally posted by Korn1699
    I have a console app with vs.net with mfc support, and I used the CTime class to get the time after and before the operation. what code would I need to use those times to find the number of seconds that it took? (This operation may take more than one day...)
    Use a couple of time_t variables to hold start and end times. At the start of your operation load the current time into a time_t variable (say for instance, StartTime). At the end load the current time into a second time_t variable (EndTime). Then use something like:
    Code:
    time_t TotalTime=EndTime-StartTime;
    Of course, you want to make checks against dates too if your operation will take more than a day, then use that to calculate a proper EndTime taking the # of days since StartTime.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    that isn't very user friendly because if they execute the code at certain times, it will not work. I was asking if there is any way, probably with mfc, that could computer the time from one point in time to another. I want to use the seconds that it takes because I was going to post this program as a way to test pc power in comparision to that of other users.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    what it does is it finds the first 10,000,000 prime numbers, and it took my 933 about 8 hours or so

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Korn1699
    that isn't very user friendly because if they execute the code at certain times, it will not work.
    How will it not work? And by "user friendly" are you referring to your experience with writing the code or to the user's experience when executing it? If you mean you, then that's just the way it is some times. There isn't always a miracle function to do everything for you. Sometimes you have to figure things out on your own. If you mean the user's experience then if you write the code properly the user should be oblivious to what's going on. All they need to know is the operation started at "this" time and ended at "that" time and "here" is the time that passed between the two.
    Last edited by jdinger; 06-06-2002 at 11:40 AM.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Yes there is something called CTimeSpan that is specialized on time difference. Look it up on http://www.msdn.microsoft.com/library/

  7. #7
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    With a CTimeSpan object you have to define the time span.

    from MSDN
    Code:
    // example for CTimeSpan::GetTotalHours
    CTimeSpan ts( 3, 1, 5, 12 ); // 3 days, 1 hour, 5 min, and 12 sec
    ASSERT( ts.GetTotalHours() == 73 );
    ASSERT( ts.GetTotalMinutes() == 4385 );
    ASSERT( ts.GetTotalSeconds() == 263112 );
    So you still need to calculate how many days/hours/minutes/seconds have passed before you use it. This will only tell him how many seconds are in a time span, not calculate the difference from start to end to give him the time span.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Create two CTime objects
    Asign the first object with the current time when the process starts and the second object with the current time when the process ends. Take the second object - the first object.

    Extract the value in what ever form you want it
    Code:
    CTime startTime = CTime::GetCurrentTime();
    
    // ... perform time-consuming task ...
    
    CTime endTime = CTime::GetCurrentTime();
    
    CTimeSpan elapsedTime = endTime - startTime;
    Last edited by Barjor; 06-06-2002 at 11:59 AM.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    thanks

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >that isn't very user friendly because if they execute the code at certain times, it will not work.

    I'm curious as to why if they execute the code at certain times, it will not work. If you could explain this, it will help others who need to time stuff.

    thanks,
    swoopy

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Didn't think aboit what I was posting

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timing basic operations in C++
    By StevenGarcia in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2007, 02:10 AM
  2. Performance Timing Function
    By rosicky2005 in forum C++ Programming
    Replies: 11
    Last Post: 05-31-2007, 03:09 PM
  3. My Timing System
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 01-01-2006, 11:43 PM
  4. Games - timing
    By Magos in forum Game Programming
    Replies: 7
    Last Post: 03-06-2004, 11:32 AM
  5. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 12:43 AM