Elapse Time Since Program Start Working

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 13
Reputation: Omher is an unknown quantity at this point 
Solved Threads: 0
Omher Omher is offline Offline
Newbie Poster

Elapse Time Since Program Start Working

 
0
  #1
Jun 5th, 2006
Hi I'm writing an application that should have when the aplication start working, I'm using the function of C++ "GetTickCount()", the problem is that I read that this function has a 'BUG' that after '49.7' days the function return '0'.
So I'm trying to write a Custom function, I thoug the following solution, to use the 'GetTickCount()' function, rigth after the application start working and save the value in a variable, after that when I will need the function again, I'll check if the 'GetTickCount()' return 0, if yes that means that should use the value saved.
The Problems:
1. When using the function and the function return '0', I'll will loose all the time that pass until the last time
2. The function is use in a lot of places(different cpp files), so in the beggining a I thoug writting a class, but the problem is that every time that I will create the object I start the timer, for the specific object.
How I can do to create like a Global Object, that all the files in the application knows it.

GetTickCount from MSDN:
The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.

From The Web:
There was a load of noise last year about some air traffic control systems implemented on Windows NT (not sure if Embedded or not) which failed after running continuously for 49.7 days. Yup, they'd not allowed for rollover.


Thanks you for your help Thanks
:eek:
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: Omher is an unknown quantity at this point 
Solved Threads: 0
Omher Omher is offline Offline
Newbie Poster

Re: Elapse Time Since Program Start Working

 
0
  #2
Jun 5th, 2006
Or maybe know the solution of writting my own custom GetTickCount
Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Elapse Time Since Program Start Working

 
0
  #3
Jun 5th, 2006
there is no bug in GetTickCount(). The reason it wraps around is that a DWORD can only hold a finite number of milliseconds and it reaches its maximum at 49.7 days. So if your program runs longer than that without being stopped and restarted, then you will have to compensate for that feature

Checking to find out if GetTickCount() returns 0 is very unlikly to occur -- you need to check if the most recent value is less than some previous value.

You might consider saving the vales in a 64-bit integer, which will give you a much longer time period. when the value returned by GetTickCount() rolls over you can easily calculate the new value (add new value to previous value and store in a 64-bit number).
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: Omher is an unknown quantity at this point 
Solved Threads: 0
Omher Omher is offline Offline
Newbie Poster

Re: Elapse Time Since Program Start Working

 
0
  #4
Jun 5th, 2006
First Thanks, you are rigth, after I posted question, I read that this is not a BUG, is exactly what you write, is a know issue.
I also read that the variable ULONGULONG, is Big enough.
If I undertand your sooution is like this, first I check if this is the first time the application is running (to save the first value), if not the first time, I try to use the GetTickCount() function, if the value is small that the already value, that means we didn't success, we add the current value to the saved value, if not we sucess, Please correct if I'm wrong.
  1. if (bFirsTime)
  2. {
  3. u_SavedValue = GetTickCount();
  4. bFirsTime = false;
  5. }
  6. else
  7. {
  8. u_CurrentValue = GetTickCount();
  9.  
  10. if( u_CurrentValue < u_SavedValue )
  11. u_SavedValue += u_CurrentValue;
  12. else
  13. u_SavedValue = u_CurrentValue;
  14. }
Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Elapse Time Since Program Start Working

 
0
  #5
Jun 5th, 2006
> Elapse Time Since Program Start Working
What do you mean?

The amount of time, as measured by the clock on the wall?

The amount of time the program has used?
If your program is low priority, say it's only using 10% of the CPU, there is a big difference between "user" time and "program" time.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Elapse Time Since Program Start Working

 
0
  #6
Jun 5th, 2006
ULONGULONG is a 64bit integer. Yes its bigger than DWORD (unsigned long) but it will have the same identical problem if your program runs long enough without stopping.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: Omher is an unknown quantity at this point 
Solved Threads: 0
Omher Omher is offline Offline
Newbie Poster

Re: Elapse Time Since Program Start Working

 
0
  #7
Jun 5th, 2006
Thanks for your help, I want to write my own GetTickCount that returns ULONGLONG, actually I want to use the GetTickCount64 function, the problem is that this works only in "Windows Vista", so I have to write my own.
The function should return: the value in number of milliseconds that have elapsed since the system was started
Please If someone know, I need your help
Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Elapse Time Since Program Start Working

 
0
  #8
Jun 5th, 2006
something like this?? But of course this won't work either when 64bit integer get data overflow, and assumes this function will get called more frequently than once or twice every 49 days!

  1. #include <windows.h>
  2. #include <limits.h>
  3.  
  4. _int64 GetMyTickCount()
  5. {
  6. static _int64 previous_count = 0;
  7. static _int64 fudge = 0;
  8.  
  9. _int64 now = GetTickCount();
  10. if(now < previous_count)
  11. {
  12. fudge = (ULONG_MAX - previous_count) + now;
  13. }
  14. previous_count = now;
  15. return now + fudge;
  16. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: Omher is an unknown quantity at this point 
Solved Threads: 0
Omher Omher is offline Offline
Newbie Poster

Re: Elapse Time Since Program Start Working

 
0
  #9
Jun 6th, 2006
Thanks, I think this will help, I have only two question:
1. I didn't undertand the line:
  1. fudge = (ULONG_MAX - previous_count) + now;
2. What will happen if I change the variable type from "_int64" to "ULONGLONG", maybe this solve my problem of overflow?
Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Elapse Time Since Program Start Working

 
0
  #10
Jun 6th, 2006
Originally Posted by Omher
Thanks, I think this will help, I have only two question:
1. I didn't undertand the line:
  1. fudge = (ULONG_MAX - previous_count) + now;
The more I think about this the more I think the value only needs to be ULONG_MAX. After roll-over only need to add ULONG_MAX to get the 64bit tick count.
  1. fudge = ULONG_MAX;


Originally Posted by Omher
2. What will happen if I change the variable type from "_int64" to "ULONGLONG"
Nothing. ULONGLONG is just a typedef for _int64 except _int64 is signed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC