Jump to content

On fast hardware the SLEEP statement do not act correctly


Recommended Posts

Hello,

I noticed on several installations that the Sleep statment causes CPU issues when the customer hardware is a powerfull one.

For example: 

While 1

    Sleep(5000)

Wend

Shall cost 0% CPU

But is the hardware is fast the Sleep(5000) costs 3 % CPU when included into a loop.

So, if 50 users start this script concurrent it overkill the CPU (which is not good of course).

In such cases, the workaround I am using is to set a much longuer delay fo such specific case, but this is not a good solution

because the script behavior is unpredictable.

Can you help me on that specific problem?

Kind regards

Dominique Benoit

 

 

Link to comment
Share on other sites

That would never cause a CPU to use 3% of anything, you're just sleeping the script for 5 seconds, which uses no CPU resources. Even on a fast computer that wouldn't cause a 3% usage, whatever a fast computer means to you.

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

Link to comment
Share on other sites

Autoit Script  have this problem at

My Patch

CPU %0

 

try it

$dll=DLlOpen("ntdll.dll")
while 1

_HighPrecisionSleep(100000,$dll)
WEnd



Func _HighPrecisionSleep($iMicroSeconds,$hDll=False)
    Local $hStruct, $bLoaded
    If Not $hDll Then
        $hDll=DllOpen("ntdll.dll")
        $bLoaded=True
    EndIf
    $hStruct=DllStructCreate("int64 time;")
    DllStructSetData($hStruct,"time",-1*($iMicroSeconds*10))
    DllCall($hDll,"dword","ZwDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct))
    If $bLoaded Then DllClose($hDll)
EndFunc
Edited by Turkishuser
Link to comment
Share on other sites

Try it with a Sleep(100), you'll see the same results. There's no reason to use a timer like that just to get the script to sleep for a few milliseconds to limit CPU activity.

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

Link to comment
Share on other sites

Hello,

I review my code.

In fact within the loop, of course I am doing something.

I had If ProcessExists($pid) Then ExitLoop

 

While 1

    If ProcessExists($pid) Then ExitLoop

    Sleep(5000)

Wend

 

I have replace it by 

 

$exitloop = False

While 1

 

$list = ProcessList()          ; hopping that this will use less CPU
   For $I = 1 To $list[0][0]
       If $pid = $list[$I][1] Then 
            $exitloop = True
            ExitLoop
       EndIf
   Next
    Sleep(5000)
If $exitloop the ExitLoop
Wend

 

So I feel that the point is the CPU usage of ProcessExists statement

We 50 concurrent script running concurrently you load the CPU by checking the processes every 5 seconds per user

I hope that the processlist statement will use less CPU

I will know it tomorrow

Thank you for you inputs :)

I do appreciate

Dominique Benoit

Link to comment
Share on other sites

Would it be possible to replace the ProcessExists() call with a WinExists() call? I've done this replacement in the past because I noticed that the ProcessExists() calls produce tons and tons of paging errors.

Link to comment
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
 Share

×
×
  • Create New...