Sign in to follow this  
Followers 0
rcmaehl

Un-stressing Tight/Recursive Loops + Other questions

6 posts in this topic

#1 ·  Posted (edited)

Okay. I've read a few threads that suggest using

Sleep(10)

in long Do...Until Loops to lower CPU usage. Is that like the official amount for best Performance/Load or something? Also, a semi-related question, How long does Sleep(0) sleep for? It has to sleep for some amount of time (probably less than a millisecond, and in the nanosecond range) because it gets executed or do all sleep(0)s get removed upon compiling?

EDIT: Removed grammatical error. :D

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

MY PROJECTS


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Give this little script a try. Run it from SciTe, run it compiled and compare the displayed time.

$a = TimerInit()
Sleep(0)
$b = TimerDiff($a)
MsgBox(0,"", $b)

As compiling means to put the script and everything needed for execution into an exe file your script is still interpreted when you run the exe.

So I guess you won't see a difference because sleep(0) will not be removed.

Edited by water

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

Give this little script a try. Run it from SciTe, run it compiled and compare the displayed time.

$a = TimerInit()
Sleep(0)
$b = TimerDiff($a)
MsgBox(0,"", $b)

As compiling means to put the script and everything needed for execution into an exe file your script is still interpreted when you run the exe.

So I guess you won't see a difference because sleep(0) will not be removed.

Wouldn't running that in a finite loop about 1000 or so times and then calculating the average give a better estimation?

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

MY PROJECTS


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

But you add the overhead of the loop. And as Sleep(0) needs very little processing time you mainly measure the loop.

See the difference:

$iTimerA = TimerInit()
$iTimerB = TimerInit()
For $i = 1 To 1000
    Sleep(0)
Next
$IResultA = TimerDiff($iTimerA)
For $i = 1 To 1000
Next
$IResultB = TimerDiff($iTimerB)
MsgBox(0,"", "1000 loops with Sleep(0) take: " & $IResultA & " milliseconds" & @CRLF & _
    "1000 loops without Sleep(0) takes: " & $IResultB - $IResultA & " milliseconds")
Edited by water

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

rcmaehl,

Soemwhere here there is a thread where Jon explained all this, but can I find it.....:rip:

I seem to remember that Sleep(10) is the lowest value that Sleep will accept - anything lower still gives you a Sleep(10) - and that this is the result of a Windows limitation rather than anything in AutoIt. I believe it was reduced from Sleep(15) at some time in AutoIt's past when the Windows API changed internally. By the way monoceres produced a UDF which had nanosecond accuracy and he found that 100ns was quite sufficient to offload his CPU - so Sleep(10) should be more than adequate. And you do realise that you do not need a Sleep in a GUIGetMsg loop as the function does its own idle? :D

As far as I know Sleep(0) is a special case which means that you give up the timeslice allocated by Windows - quite what effect that will have on your script I have no idea. :)

I hope this helps. I will keep looking for that thread.... :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Putting a sleep into a Do...Until loop will only help with CPU load depending upon what you're doing inside that loop. If it's running a For...Next loop inside that loop 10000 times, every time through the loop, that reads files into arrays it probably won't have any effect on CPU load. If all you're doing is looking for a pixel color to appear it might help somewhat.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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