 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Torben Guest
|
Posted: Sat Jul 03, 2004 9:15 pm Post subject: Does constantly running loops affect computer performance? |
|
|
AutoHotKey is a fabulous program.
Even though I am a novice in programming I succeeded in making a simple CAPSLOCK INDICATOR in the program tray. I am using a wireless keyboard without led-indicators for Num-, Caps- or Scroll-Lock. The lock-indicator that came with the keyboard is rather clumsy and occupies quite a bit of the lower right part of the screen.
Here is my code
| Code: |
; CAPSLOCK INDICATOR
; Tested on WinXp sp1
; Torben
; 03-07-2004
;
; You need an icon for ON and one for OFF
LOOP
{
GetKeyState, state, CapsLock, T ; D if CapsLock is ON or U otherwise.
IfEqual state, D
{
Menu, Tray, Icon, on.ico, 1
TrayTipText = CapsLock is ON
}
else
{
Menu, Tray, Icon, off.ico, 1
TrayTipText = CapsLock is OFF
}
IfNotEqual Var1, %state%
{
TrayTip, ,%TrayTipText%
SetTimer, DisplayTrayTip, 1000
}
Var1 = %state%
}
DisplayTrayTip:
SetTimer, DisplayTrayTip, Off
TrayTip
return
|
If you want to try it, you might make it even simpler by deleting the TrayTip part of the code. In fact only these few lines code are needed:
| Code: |
LOOP
{
GetKeyState, state, CapsLock, T ; D if CapsLock is ON or U otherwise.
IfEqual state, D
Menu, Tray, Icon, on.ico, 1
else
Menu, Tray, Icon, off.ico, 1
}
|
Now to my question:
As I use a constantly running loop which the computer probably goes thru hundred of times every second, I worry if the script uses too much of the computers resources on performing this rather simple task. Is that worry reasonable and if so, does anyone have a proposal for an alternative code, or are there some sort of directive that I can give to minimize the scripts load on the computer?
Thanks
/Torben |
|
| Back to top |
|
 |
Rajat
Joined: 28 Mar 2004 Posts: 1717
|
Posted: Sat Jul 03, 2004 9:30 pm Post subject: |
|
|
yes this can be a bit of overkill. just put 'sleep, 500' inside the loop and that's more than enough rest for the CPU. even a 'sleep, 100' will do. _________________
 |
|
| Back to top |
|
 |
Rajat
Joined: 28 Mar 2004 Posts: 1717
|
Posted: Sat Jul 03, 2004 10:07 pm Post subject: |
|
|
by the way u can use a tooltip just above the system tray to show the status of all three locks using minimal screen space.
remember to use:
coordmode, tooltip, screen _________________
 |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Sat Jul 03, 2004 10:39 pm Post subject: |
|
|
Nice script. Here's another way to do it that doesn't use a loop. But it does use the keyboard hook, so if nothing else in the script needs the hook, it will take up more memory this way:
| Code: | ~CapsLock::
GetKeyState, state, CapsLock, T ; D if CapsLock is ON or U otherwise.
if state = D
{
Menu, Tray, Icon, on.ico
TrayTip, , CapsLock is ON
}
else
{
Menu, Tray, Icon, off.ico
TrayTip, , CapsLock is OFF
}
Sleep, 1000
TrayTip
return |
Btw, even a Sleep 10 would probably cause a short loop to use 0% CPU time (Sleep 10 is better if response time is important). |
|
| Back to top |
|
 |
Torben Guest
|
Posted: Sun Jul 04, 2004 7:38 am Post subject: |
|
|
Thank you.
I am surprised to hear that the sleep command reduces the work load on the computer, but that’s probably because I know too little about how computers work.
I tested sleep with more values between 10 and 1000. With sleep 1000 on my computer the reaction time was a little slow, especially when you have to press CapsLock twice for instance when you pressed it accidentally and want to toggle it back at once. Up till 200 it worked quite well.
Thank you Chris for the alternative suggestion. A minor problem is that the on/off icon does not appear until you have pressed CapsLock once.
If the keyboard hook for CapsLock takes up more memory I guess it will be even worse when you add a NumLock and a ScrollLock indicator. But which solution strains the computer the most: 3 keyboard hooks or 3 repeating loops (with sleep 200)? Is measuring CPU time the appropriate way to decide?
/Torben |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Sun Jul 04, 2004 1:18 pm Post subject: |
|
|
| Quote: | | If the keyboard hook for CapsLock takes up more memory I guess it will be even worse when you add a NumLock and a ScrollLock indicator. |
No, the hook is only needed once. So once it's installed, it can be used for any number of things within the same script without using more memory. The hook uses approximately 750K of additional RAM when installed. This does not increase significantly if you install a 2nd hook (such as the mouse hook), since they both share the same memory.
| Quote: | | But which solution strains the computer the most: 3 keyboard hooks or 3 repeating loops (with sleep 200)? Is measuring CPU time the appropriate way to decide? |
I think the below is the best solution if you don't mind giving up the 750K of memory (especially if you wind up adding other hotkeys to this script that need the hook anyway, or if you merge this script in with some other). You shouldn't need to worry about CPU time with this or any of the other methods, but if you want to check, you can watch the "CPU time" column in task manager after running the script for a day. Depending on CPU speed, the usage will only be a few seconds even after a day, and probably less than that.
| Code: |
Gosub, ~CapsLock ; Run the hotkey once upon startup to set the proper icon.
return
~CapsLock::
GetKeyState, state, CapsLock, T ; D if CapsLock is ON or U otherwise.
if state = D
{
Menu, Tray, Icon, on.ico
TrayTip, , CapsLock is ON
}
else
{
Menu, Tray, Icon, off.ico
TrayTip, , CapsLock is OFF
}
SetTimer, TrayTipOff, 1000 ; Timer is better since it allows key to be pressed during the interval.
return
TrayTipOff:
SetTimer, TrayTipOff, off
TrayTip
return |
Also, you can copy & paste the entire ~CapsLock:: to make one that's almost the same for Scroll & Numlock, if desired. |
|
| Back to top |
|
 |
Torben Guest
|
Posted: Sun Jul 04, 2004 3:31 pm Post subject: |
|
|
Thank you so much for the explanations and the script.
/Torben |
|
| Back to top |
|
 |
Atomhrt Guest
|
Posted: Sat Jul 10, 2004 3:48 pm Post subject: |
|
|
Here's my version for both Capslock and Numlock. Note that ico files are not used for obvious reasons. I used a 3 second display of the tooltip.
| Code: | ;===============================================
Capsstr=""
Numstr=""
Gosub, ~CapsLock ; Run the hotkey once upon startup to set the proper icon.
return
~CapsLock::
~NumLock::
GetKeyState, state, CapsLock, T ; D if CapsLock is ON or U otherwise.
if state = D
{
Capsstr=CapsLock is ON
}
else
{
Capsstr=CapsLock is OFF
}
GetKeyState, state, NumLock, T ; D if NumLock is ON or U otherwise.
if state = D
{
Numstr=NumLock is ON
}
else
{
Numstr=NumLock is OFF
}
Gosub, DisplayTip ; Show Keys Status
SetTimer, TrayTipOff, 3000 ; Timer is better since it allows key to be pressed during the interval.
return
DisplayTip:
TrayTip, , %Capsstr%`n%Numstr%
return
TrayTipOff:
SetTimer, TrayTipOff, off
TrayTip
return
;=============================================== |
|
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Sat Jul 10, 2004 7:10 pm Post subject: |
|
|
| Looks good. I edited the post to make the section "code" vs. "quote" to preserve indentation. |
|
| Back to top |
|
 |
Torben Guest
|
Posted: Mon Jul 12, 2004 9:55 am Post subject: Does constantly running loops affect computer performance? |
|
|
The only disadvantage by using Atomhrt's script is that you can only spot the changes in the toggles for 3 seconds, while my intention was to make something like a led-indicator that constantly indicated whether or not the CapsLock (or NumLock) was toggled. You might of course modify my (or Chris’) script to show 4 four different versions of one icon depending on the combinations of CapsLock and NumLock status -- if you don't like to have an icon for each.
Atomhrt's script can be improved so that the CapsLock- and NumLock-status is displayed when the mouse hovers over the tray icon. Insert the code below just after the last “}” and before the “Gosub, DisplayTip ; Show Keys Status “:
| Code: |
Menu, Tray, Tip, %Capsstr%`n%Numstr% ; Changes the tray icon's tooltip -- which is displayed when the mouse hovers over it
|
Torben |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|