[RESOLVED] Is GetTickCount() Safe ?
We all know that GetTickCount returns a number as (As long).
And a (As long) variable does not exceed this number: 2147483647.
So what's going to happen if I make a server that uses GetTickCount(), and that it exceeds this number? Will the server crash ? Or GetTickCount() will start all over again from Zero ? :ehh:
Re: Is GetTickCount() Safe ?
it will start again with 0.
Re: Is GetTickCount() Safe ?
Are you sure? How much time will it take to reach the limit??
Re: Is GetTickCount() Safe ?
GetTickCount would actually go to negative numbers first, it is an unsigned number. VB6's Long is signed so the number would appear negative once the highest bit is active. The number is positive up to 24 days 20 hours 31 minutes 23 seconds and 647 ms. After that it'll be negative the same amount of time. Then it returns back to positive, starting from 0.
Basically it is not safe for counting a long period of time, only short times. High performance counter is a better choice, and is more precise (far closer to 1 ms).
Code:
' Timings.bas
Option Explicit
Private Declare Function QueryPerformanceCounter Lib "kernel32" (X As Currency) As Boolean
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (X As Currency) As Boolean
Dim m_Time As Double
Dim m_TimeFreq As Double
Dim m_TimeStart As Currency
Public Property Get Timing() As Double
Dim curTime As Currency
QueryPerformanceCounter curTime
Timing = (curTime - m_TimeStart) * m_TimeFreq + m_Time
End Property
Public Property Let Timing(ByVal NewValue As Double)
Dim curFreq As Currency, curOverhead As Currency
m_Time = NewValue
QueryPerformanceFrequency curFreq
m_TimeFreq = 1 / curFreq
QueryPerformanceCounter curOverhead
QueryPerformanceCounter m_TimeStart
m_TimeStart = m_TimeStart + (m_TimeStart - curOverhead)
End Property
Usage:
Timing = 0
... do your stuff...
MsgBox "This took " & Format$(Timing, "0.00000") & " seconds!"
Re: [RESOLVED] Is GetTickCount() Safe ?
Woow ! Omg I love this, so precise!!! Thank you so much!
Re: [RESOLVED] Is GetTickCount() Safe ?
Fixed the post a bit: the results are in seconds and not milliseconds as I previously wrote. You can get milliseconds by multiplying with 1000.
Re: [RESOLVED] Is GetTickCount() Safe ?
Hold on a (milli) sec,
Just want to spout my bilge regarding QueryPerformanceCounter/Frequency.
As far as I understand it is a great API for measuring very quick 'things' to a high resolution. Its resolution is much higher than any other timing API that I know of.
Its not very good at measuring long periods of time. For long periods GetTickCount() is better.
As far as I understand QPC derives its data from chip cycles (RDTSC). GTC derives its data from a crystal oscillator.
They both eventually turn negative and roll over.
Edit: For clarity, QPC loses/gains time over long periods. If your watch used QPC it would not be very good at getting you to work on time.
Re: [RESOLVED] Is GetTickCount() Safe ?
Oh my god :s
So nothing is really safe???
That explains why some game servers crash due to an unknown problem (it must be it, I think.)
What to do :confused:
Re: [RESOLVED] Is GetTickCount() Safe ?
They are both safe. They both reliably do what it says on the box, i.e. rollover eventually.
I'm not in a place to look at the problem of using them in detail right now but off the top of my head...
Code:
MyTime = GetTicCount() And &h7fffffff
If MyTime < LastTime Then ItRolledOverSoDoSomething()
LastTime = MyTime
Re: [RESOLVED] Is GetTickCount() Safe ?
Milk: however you forgot QueryPerformanceCounter returns a 64-bit value. Assuming the frequency is upto 1 ms (which it actually rarely is, I think), it would take 106751991167 days until the positive range ended.
Re: [RESOLVED] Is GetTickCount() Safe ?
You're right it does take a long time to rollover. I wanted to make the point that it's not good at keeping time over long periods. By long periods I mean hours.
It's also unreliable on multi-core processors, unreliable on systems that vary the chip speed, and unreliable on overclocked systems.
Your Calc is not quite right, and obviously it will vary from system to system, on mine it rollovers every ((922337203685477.5807 / 279304.0000) / 86400) = 38220 days (ahem)
...But, I was trying to make a point over inaccuracy over time.
Re: [RESOLVED] Is GetTickCount() Safe ?
Are you sure you calculated right? On my machine QueryPerformanceFrequency gives "just" 273.4501 so 279304.0000 would mean an insane amount of ticks per second.
Also, I've been assuming here that the problem is use of the function over a long period of time being on... I'm not saying the function should be used to measure long periods of time, just that it can be used much more safely than GetTickCount as it is about nearly impossible for it to cause failure only because of a switch from positive to negative.
Re: [RESOLVED] Is GetTickCount() Safe ?
This...
Code:
Dim curfreq As Currency
Dim curQpc As Currency
QueryPerformanceFrequency curfreq
QueryPerformanceCounter curQpc
Debug.Print "freq"; curfreq
Debug.Print "counter"; curQpc
Debug.Print "seconds"; curQpc / curfreq
Debug.Print "minutes"; (curQpc / curfreq) / 60
Gives me this
Code:
freq 279305
counter 477166730.5424
seconds 1708.40740603426
minutes 28.4734567672377
It's an old p4 ~2793 Mhz.
Re: [RESOLVED] Is GetTickCount() Safe ?
Oh well, that would still require 100 years of uptime until it hits the negative :)