Timer difference in running the same code

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
393
Office Version
  1. 365
Platform
  1. Windows
Gurus,
for below code, timer takes down 10 records but 2 records are 1 sec lower than the other 8. Is there any reason behind this or this is within the deviation range? Thank you.

VBA Code:
Sub pop()
For x = 1 To 10
st = Timer
d = Now + TimeValue("00:00:03")
For Each cell In Range(Cells(18, 1), Cells(18, 10))
Do
Loop Until d <= Now
d = Now + TimeValue("00:00:03")
cell.Interior.ColorIndex = 3
Next cell
st = Timer - st
Range("o100").End(xlUp).Offset(1, 0) = st
Range(Cells(18, 1), Cells(18, 10)).Clear
Next x
End Sub



timer
30.86328​
31.99609​
31.98828​
31.99609​
30.99609​
31.99609​
31.99609​
31.99609​
31.99609​
31.99609​
 
run the code at exact 30.000 sec, I don't know.
Here with a small error of around 20-30 msec, 30.030 sec.

So add instead of 3.000 sec 2.98 sec and surprising, now the error is -0.15 sec.
No logic, but it isn't worth the effort to find the reason.
Perhaps if you have time ...
VBA Code:
Sub pop()

     Set c = Cells(18, 1)
     dag = 86400                                                '86400 = 1 day in seconds
     st = Timer
     Range("O1:O100").ClearContents

     Do

          b1 = (WorksheetFunction.Median(1, 10, i) <> i)        'flag : i is in the interval 1-10, otherwise
          b2 = True                                             'do 1 write in the next small loop

          Do
               If b1 Then
                    i = 1
                    c.Resize(, 10).Clear                        'clear red cells
                    Range("O100").End(xlUp).Offset(1, 0) = Timer     'the real timer-value
                    b1 = False
               End If

               If b2 Then
                    With c.Cells(1, i)
                         .Value = Timer & Chr(10) & st          'write the real timer-value
                         .Interior.ColorIndex = i + 2           'make the cell red
                    End With
                    st = Timer - Int(Timer / dag) * dag + 2.98    'in case of last chrono before midnight, next passed midnight
                    b = (st > dag)
                    i = i + 1
                    b2 = False
               End If

               DoEvents
          Loop While Timer - b * dag < st                       'CAUTION : b is 0 (false) or -1 (true)

     Loop
End Sub
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Your first one returns a Single, the second a Double, which may account for the small difference. Frankly, if you need something this precise I think you're probably using the wrong language anyway, but you might want to look at the gettickcount api.
 
Upvote 0
It appears that your new question is just really a continuation of your old one, so I merged the two threads together.
In the future, please do NOT start a new thread on the same topic - just continue on in the original one.
Only start a new thread for a new topic.
 
Upvote 0
use now() instead now, to get smallest difference.
there is no difference between now and now(), they are both on a second base.
And for the rest, timer adds 2 extra digits, but with doubts on the last.
Excel is a calculation program, not a chronometer.

This a repeated question, in another one, i gave you a solution where your 30.89-31.99 range was reduced to a 30.010-30.030 range.
But was that worth the effort ? You make your computer do stupid loops, "busy doing nothing".

As i stated before, that's the difference between an old clock which jumps in seconds and another one where the seconds runs smoothly and you have to measure a timeperiod. In official competitions with manual chronometers, i believe the accuracy used as 1/2 sec.
 

Attachments

  • Schermafbeelding 2022-01-13 075456.png
    Schermafbeelding 2022-01-13 075456.png
    12.3 KB · Views: 4
Upvote 0
i was wrong in now=now()
 

Attachments

  • Schermafbeelding 2022-01-13 082634.png
    Schermafbeelding 2022-01-13 082634.png
    14.5 KB · Views: 9
Upvote 0
sorry, i was wrong being wrong, so now()=now, until somebody else proves the opposite.
 
Upvote 0
Testing with:
VBA Code:
Sub test()
Debug.Print (Now - Date) * 24 * 60 * 60, Timer, (Now() - Date) * 24 * 60 * 60
End Sub
in which: now = now(),
but timer is later
No idea why?

Capture.JPG
 
Upvote 0
that is bull **** (apparently censored word), try in an empty sheet
1.000 timestamps in almost 7 seconds.
the most relevant is the 3rd (now-now())*86400 and is always 0 !
timer is the continuous running clock second pointer.
the other 2 are tick-wise clock second pointers
VBA Code:
Sub test()
     Set dict = CreateObject("scripting.dictionary")
     Do
        Debug.Print (Now - Date) * 24 * 60 * 60, Timer, (Now() - Date) * 24 * 60 * 60, (Now - Now()) * 86400, WorksheetFunction.Round((Now - Date) * 24 * 60 * 60, 3), WorksheetFunction.Round(Timer, 3), WorksheetFunction.Round((Now() - Date) * 24 * 60 * 60, 3)
          dict.Add dict.Count, Array(dict.Count + 1, (Now - Date) * 24 * 60 * 60, Timer, (Now() - Date) * 24 * 60 * 60, (Now - Now()) * 86400, WorksheetFunction.Round((Now - Date) * 24 * 60 * 60, 3), WorksheetFunction.Round(Timer, 3), WorksheetFunction.Round((Now() - Date) * 24 * 60 * 60, 3))
     Application.StatusBar = dict.Count
     For i = 1 To 25
     DoEvents
     Next
     Loop While dict.Count < 1000
     Range("A2").Resize(dict.Count, 8).Value = Application.Index(dict.items, 0, 0)
  
End Sub
 

Attachments

  • Schermafbeelding 2022-01-13 125116.png
    Schermafbeelding 2022-01-13 125116.png
    11.9 KB · Views: 4
Last edited:
Upvote 0

Forum statistics

Threads
1,215,637
Messages
6,125,965
Members
449,276
Latest member
surendra75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top