2010/5/30 Johnny Rosenberg <gurus.knu...@gmail.com>:
> 2010/5/30 Bernard Marcelly <marce...@club-internet.fr>:
>> Message de Konstantin Tokarev  date 2010-05-29 19:10 :
>>>
>>>> On MS-Windows the result is in milliseconds, with a precision of 16 ms
>>>> due to IBM PC hardware design.
>>>
>>> Does it mean that results of OOo Basic scripts may be platform-dependent?
>>>
>>
>> Help says it depends on OS. I have not tested on other OS than MS-Windows,
>> nor on other HW platform, e.g. Mac.
>> Help shows how to count the ticks for a given delay.
>>
>> This code shows on PC/MS-Windows that GetSystemTicks does not increment by
>> 1.
>>
>> Dim t1 As Long, t2 As Long, n As Long
>> n = 0
>> t1 = GetSystemTicks
>> Do
>>  t2 = GetSystemTicks -t1
>>  n = n+1
>> Loop Until t2 <> 0
>> MsgBox("t2 = " & t2 & "   n = " & n)
>>
>> Regards
>>  Bernard
>
>
> Maybe I was wrong about the getSystemTicks() function. It seems like
> it actually measures time in ms, not just CPU time. At least my
> experiments point in that direction:
>
> REM  *****  BASIC  *****
>
> Option Explicit
>
> Sub Main
>        Dim t1 As Double, t2 As Double, n As Long, st as Long
>        n=0
>        t1=now()
>        st=GetSystemTicks()
>        Do
>                t2=now()
>                n=n+1
>        Loop Until (t2-t1)*86400>=100 ' Approximately 100 seconds
>        MsgBox("Ticks/s: " & GetSystemTicks()-st & chr(13) & _
>                "Loop: " & n & chr(13) & "t1= " & t1 & chr(13) & _
>                "t2= " & t2 & chr(13) & "t2-t1= " & t2-t1 & _
>                chr(13) & "In s: " & (t2-t1)*86400)
> End Sub
>
> Maybe something is wrong with my code, because sometimes it runs for
> an extra second. Run it in just one second instead of 100:
>
> REM  *****  BASIC  *****
>
> Option Explicit
>
> Sub Main
>        Dim t1 As Double, t2 As Double, n As Long, st as Long
>        n=0
>        t1=now()
>        st=GetSystemTicks()
>        Do
>                t2=now()
>                n=n+1
>        Loop Until (t2-t1)*86400>=1 ' One second only
>        MsgBox("Ticks/s: " & GetSystemTicks()-st & chr(13) & _
>                "Loop: " & n & chr(13) & "t1= " & t1 & chr(13) & _
>                "t2= " & t2 & chr(13) & "t2-t1= " & t2-t1 & _
>                chr(13) & "In s: " & (t2-t1)*86400)
> End Sub
>
> Now the ticks result should be somewhere between 0 and 999, right? It
> usually is, but now and then it's almost 2000. The n variable tells us
> how many times it looped, like in your example above, and it runs
> quite a few times for one tick, so why doesn't it realize that it's
> time to stop until (t2-t1)*86400 almost reaches 2?
> Yes, a bit out of topic, I know, but still interesting to know…
>
> For example one of the outputs is like this:
> Ticks/s: 1605
> Loop: 43638
> t1= 40328,4520717593
> t2= 40328,4520949074
> t2-t1= 2,31481462833472E-05
> In s: 1,99999983888119
>
> Why didn't it stop until (t2-t1)*86400≈2? It should have been ⩾1
> thousands of times before that, shouldn't it?
>
> Johnny Rosenberg
>

Figured it out…

REM  *****  BASIC  *****

Option Explicit

Sub Main
        Dim t1 As Double, t2 As Double, t2old as Double
        Dim n As Long, nold as Long, st as Long
        n=0
        t1=now()
        st=GetSystemTicks()
        Do
                t2old=t2
                t2=now()
                nold=n
                n=n+1
        Loop Until (t2-t1)*86400>=1
        MsgBox("Ticks/s: " & GetSystemTicks()-st & chr(13) & _
                "Loop: " & n & chr(13) & "t1= " & t1 & chr(13) & _
                "t2= " & t2 & chr(13) & "t2-t1= " & t2-t1 & _
                chr(13) & "In s: " & (t2-t1)*86400 & chr(13) & _
                chr(13) & "Loop-1: " & nold & chr(13) & _
                "t2old= " & t2old & chr(13) & "t2old-t1= " & _
                t2old-t1 & chr(13) & "In s: " & (t2old-t1)*86400)
End Sub

Due to precision error (t2-t1)*86400 is almost never an integer.
Sometimes it is something.00000something and sometimes it is
something.99999something, so of it after one second is 0.9999993568 it
is not ⩾1 so it loops a couple of thousands times more until it reach
the next step which is something like 1,99999993543…

Easy fixed by rounding the result of (t2-t1)*86400 to 0 decimals, I
think. Even easier to do something like this, though:
REM  *****  BASIC  *****

Option Explicit

Sub Main
        Dim t1 As Double, t2 As Double, t2old as Double
        Dim n As Long, nold as Long, st as Long
        n=0
        t1=now()
        st=GetSystemTicks()
        Do
                t2old=t2
                t2=now()
                nold=n
                n=n+1
        Loop Until (t2-t1)*86400>0.9
        MsgBox("Ticks/s: " & GetSystemTicks()-st & chr(13) & _
                "Loop: " & n & chr(13) & "t1= " & t1 & chr(13) & _
                "t2= " & t2 & chr(13) & "t2-t1= " & t2-t1 & _
                chr(13) & "In s: " & (t2-t1)*86400 & chr(13) & _
                chr(13) & "Loop-1: " & nold & chr(13) & _
                "t2old= " & t2old & chr(13) & "t2old-t1= " & _
                t2old-t1 & chr(13) & "In s: " & (t2old-t1)*86400)
End Sub


Johnny Rosenberg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@api.openoffice.org
For additional commands, e-mail: dev-h...@api.openoffice.org

Reply via email to