Sign in to follow this  
Followers 0
yupepa

[SOLVED] Strange behavior of TimerDiff

33 posts in this topic

#1 ·  Posted (edited)

Hi,

while running a script, i sometimes need a short delay. I tried it with sleep, but received a strange behavior depending on the hardware.

Just a simple script like

$begin = TimerInit()
Sleep(5000)
MsgBox(0, "Timer", "Duration = "& TimerDiff($begin))

gives a duration of 5004 on a Pentium with 4CPU, 3GHz, on an Intel Core2 Duo, E8400 3GHz differs each time i run the script,

but allways much lower, between 200 and 500!

Even compiling the script on the second PC didn't change anything????

Funny thing, the real duration is correct, only the output differs

Any ideas, do i miss anything???

Regards

Paul

Edited by yupepa

Share this post


Link to post
Share on other sites



No yupepa , it`s not strange

the result is : pause 5000 as you want + the time for do 3 lines

you can try adding more lines and the result will be bigger as this :

$begin = TimerInit()
Sleep(5000)
for $i=1 to 100
$i=$i
next
MsgBox(0, "Timer", "Duration = "& TimerDiff($begin))

Share this post


Link to post
Share on other sites

I cannot reproduce you problem here.

On all PCs I tested the TimerDiff works s designed.

@bestsmr: I guess his issue is not the 4ms difference but the fact that his Core2Duo reports 200 - 500 ms instead of 5000


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

One of the outputs ....

Regards

Paul

Share this post


Link to post
Share on other sites

There should be a screen print... but it seems not to work....

Whats the best way to insert a screenprint???

Paul

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Guys, the issues your having are due to the structure of PCs. It is due to structure of the operating system as a "preemptive multitasking system".

Ultimately, your computer can only do a set amount of things at once. If your computer is a dual core then it can only do 2 things at once, and so on.

So what the OS does is stops your program executing on CPU, and hands over control to another running program. Your program is completely frozen until your turn (timeslice) comes around again and the OS gives your program control of the CPU.

What does this have to do with timing? Well if you have a loop to do timing (as you have done above), whats to garrantee that the OS will not take over control before the end? that can completely screw up timing. It can be anywhere between 13ms to 600ms before your program gets control again. Hence timing is broken.

Same thing with sleep. Sleep is NOT meant to be a precise measure of timing. If you are using sleep, what you are really saying to the OS is "I dont need the CPU for x milliseconds, do whatever you want and hand it back to me no earlier than x milliseconds."

So dont worry about timing. There is nothing you can do to garrantee CPU time and thus garrantee timer are accurate.

Edited by twitchyliquid64

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Share this post


Link to post
Share on other sites

What you're saying is true for results > the 500ms he entered into his sleep() function.

I can only guess how the TimerDiff function works: It just compares two timestamps: the actual timestamp and the given timestamp. So the OP states that his script really does the 5000ms sleep but the TimerDiff() reports 200 - 500ms.

On the other hand you're right about the accuracy of timing. There's really no guarantee. :)


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

On my dual core I sometimes get a return time of less than 50ms smaller than the minimum time defined by Sleep(5000). Nothing so extreme as reported in the OP.

15 runs:

5006.84854272567

4999.54640349575 <

5000.51348143599

5000.50838484605

5000.66128254412

5006.91161302612

5003.10191204942

5005.31000963893

4999.82034520478 <

4999.84391693323 <

4999.81588568858 <

5003.13121744155

4997.92250252759 <

5003.58099150335

5001.60224046094

Edit: I'm also on a 64bit machine.

Edited by John

Share this post


Link to post
Share on other sites

I also get results between 4992 and 5005. Values smaller 5000 are possible because of the divergence introduced when converting the integer to the internal binary representation.

The result also includes the time the Autoit interpreter needs to parse and execute the statements following "TimerInit" (Sleep, MsgBox, TimerDiff).


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

There are a bunch of threads on the accuracy of sleep and timing methods.

I found this method to be quite accurate.

Func _Sleep($sTime)
$timer=TimerInit()
Sleep($sTime - 20)
Do
$tmp = TimerDiff($timer)
Until $tmp >= $sTime
EndFunc

Share this post


Link to post
Share on other sites

I just tried the direct dll method as described by monoceres

$dll=DllOpen("kernel32.dll")
$timer=TimerInit()
DllCall($dll,"none","Sleep","dword",1000)
MsgBox(0, "Timer2", "Laufzeit = "& TimerDiff($timer))

the result was nearly the same

What is strange to me is that the sleeping time is accurate, only the output of the TimerDiff is not correct.

Share this post


Link to post
Share on other sites

I really can't believe ya'll are fussing over a -3 to 7 milliseconds of display.

Pretty confident if I made a true test myself it'd be even closer.

The idea that it is that close overall, to me, is quite impressive.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

No i dont speak about milliseconds, i need a delay of 0,5 to 2 seconds until the GUI is build up completely and i can not use WinWaitActive.

By the way i am only curious about the wrong output of TimeDiff, since the time is whithin some milliseconds correct.

Sleep (5000) => sleeping time measured about 5s, output TimeDiff => between 200 and 500!

Share this post


Link to post
Share on other sites

I would need to see exact script you run and exact output you get.

I'm not interesed in "betweens" nor parts of the bigger scripts.

Also you need to say what version of AutoIt you run that code on and what system you run it on.

It would also be nice if other people would stop talking about the wrong and unrelated things.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

The script is as mentioned in the begining of the thread, I just updated Autoit to the latest version 3.3.8.1, on Intel Core2 Duo, E8400 3GHz.

Output within the MessageBox as mentioned before, varies between 200 and 500.

Share this post


Link to post
Share on other sites

Are you stupid person? Did you read my post? Do you want to know what the problem is or not?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

What is your problem?

This is the complete script!

Do you really want a picture of the messagebox with a number? i attached it.

I told you the version and system data!

What are you missing????

post-74110-0-22041400-1341575325_thumb.p

Share this post


Link to post
Share on other sites

Could someone write simple script for yupepa member that would show us what his operating system is, AutoIt version, system bitness, AutoIt bitness, and all other important stuff he should show together with the result of TimerDiff() in one message box that he can take a screenshot of?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Hi, Yupepa. Try something like this to give Trancexx the information she needs. Post the screenshot in your reply, please.

$begin = TimerInit()
Sleep(5000)
MsgBox(0, "For Yupepa", "OS: " & @OSVersion & @CRLF & "Architecture: " & @OSArch & @CRLF & "AutoIt Ver: " & @AutoItVersion & @CRLF & "Duration = " & TimerDiff($begin))

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Add the AutoIt bitness:

$begin = TimerInit()
Sleep(5000)
MsgBox(0, "For Yupepa", "OS: " & @OSVersion & @CRLF & "Architecture: " & @OSArch & @CRLF & "AutoIt Ver: " & @AutoItVersion & @CRLF & "@AutoItX64: " & @AutoItX64 & @CRLF & "Duration = " & TimerDiff($begin))

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0