Jump to content



Photo

How to minimize CPU usage


  • Please log in to reply
7 replies to this topic

#1 Baritonomarchetto

Baritonomarchetto

    Seeker

  • Active Members
  • 28 posts

Posted 05 July 2012 - 08:08 AM

Hi All,

I am trying to figure out how to reduce the CPU usage of a script like this:

#include <Misc.au3> Local $hDLL = DllOpen("user32.dll") While 1 If WinActive("[CLASS:MYWIN]") Then    function() EndIf Sleep(80) ; keep the CPU cool WEnd Func function() If _IsPressed("30", $hDLL)  Then ... ... ... EndIf EndFunc


It relies on key pressing so i suppose i cannot go high with "Sleep" (which, i suppose, could reduce the usage by reducing the number of cycles per second). Any idea?

Thanks :)





#2 water

water

    ?

  • MVPs
  • 10,662 posts

Posted 05 July 2012 - 08:18 AM

Can you tell us what you try to achieve? Maybe there is a simpler solution.

UDFs:

Active Directory (2012-10-12 - Version 1.3.0.0 released) - Download - General Help & Support - Example Scripts - Wiki

OutlookEX (2012-10-07 - Version 0.9.0.0 released) - Download - General Help & Support - Example Scripts - Wiki

ExcelChart (2013-01-21 - Version 0.3.1.1 released) - Download - General Help & Support - Example Scripts

WordEX (2012-12-29 - Version 1.3 released) - Download

ExcelEX (2013-05-11 - Alpha 4 released) - Download


#3 Baritonomarchetto

Baritonomarchetto

    Seeker

  • Active Members
  • 28 posts

Posted 05 July 2012 - 08:32 AM

I want that, while a defined program is active, my button-press related function is triggered... the target program can go unactive then active, and so on, so i suppose there are no better solutions other than set "WinActive" into the "while" loop, which probably is the heaviest part, isn't it?

Edited by Baritonomarchetto, 05 July 2012 - 08:32 AM.


#4 water

water

    ?

  • MVPs
  • 10,662 posts

Posted 05 July 2012 - 08:41 AM

I would raise the sleep time from 80 milliseconds to at least 250 to keep the CPU cool. You would have to wait 1/4 second maximum. That should be acceptable.

UDFs:

Active Directory (2012-10-12 - Version 1.3.0.0 released) - Download - General Help & Support - Example Scripts - Wiki

OutlookEX (2012-10-07 - Version 0.9.0.0 released) - Download - General Help & Support - Example Scripts - Wiki

ExcelChart (2013-01-21 - Version 0.3.1.1 released) - Download - General Help & Support - Example Scripts

WordEX (2012-12-29 - Version 1.3 released) - Download

ExcelEX (2013-05-11 - Alpha 4 released) - Download


#5 Baritonomarchetto

Baritonomarchetto

    Seeker

  • Active Members
  • 28 posts

Posted 05 July 2012 - 09:07 AM

Thanks :)

#6 John

John

    Polymath

  • Active Members
  • PipPipPipPip
  • 223 posts

Posted 05 July 2012 - 09:23 AM

On a lot of scripts I do I turn GUIOnEventMode on and use Sleep(2100000000) instead of using a While loop, which is a good part of a month before the program will exit on its own. If you want it to run longer the same sleep time in a loop will work. The Gui can also be created and destroyed with this mode via a HotKey, making the code do almost nothing whatsoever except Sleep until you act on it. Even with the Gui active there is no active While loop polling the Gui.

#7 Zedna

Zedna

    AutoIt rulez!

  • MVPs
  • 8,315 posts

Posted 05 July 2012 - 10:39 AM

Use diferent Sleep time when your window is/isn't active

While 1     If WinActive("[CLASS:MYWIN]") Then        function()         Sleep(25) ; keep the CPU cool     Else         Sleep(250) ; keep the CPU cool     EndIf WEnd


#8 twitchyliquid64

twitchyliquid64

    Peace. Always.

  • Active Members
  • PipPipPipPipPipPip
  • 519 posts

Posted 05 July 2012 - 12:41 PM

Use diferent Sleep time when your window is/isn't active

While 1 If WinActive("[CLASS:MYWIN]") Then function() Sleep(25) ; keep the CPU cool Else Sleep(250) ; keep the CPU cool EndIf WEnd

Good idea!



Also, on larger projects with lots of windows, I find its good to nest your If statements for control clicks in IF statements that test if the message comes from a specific window. That way you avoid doing ALL the tests every iteration.

While 1 $msg = GUIGetMsg(1) if $msg[0] == $win1 then ;...IF statements for testing controls on first window elseif $msg[0] == $win2 then ;...Likewise for second window. Endif Wend

Edited by twitchyliquid64, 05 July 2012 - 12:41 PM.

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users