Xibalba Posted February 4, 2011 Share Posted February 4, 2011 My program is very dependent on running fast. I currently have this code running about 8 times each second (at the end of an endless loop until exiting):If NOT (StringLen($System_Log) == 0) PrintLogToTxt() $System_Log = "" EndIfNote: $System_Log is a global string containing between 0 to 150,000 characters each loop (varies).This should be faster than using If NOT ($System_Log == "")orIf $System_Log <> ""..but I would like confirmation on this and/or tips on further optimization.I'm thinking about running the first mentioned code every 2, or 4 loops instead of every. Would this speed up things? (the $System_Log string would be 4x larger)Question 2)I also have gazillions of Case, Switch, and If-Else statements. After some research into this - "If" is obviously the slowest. But which is the fastest in general - case or switch?Thanks in advance Link to comment Share on other sites More sharing options...
KaFu Posted February 4, 2011 Share Posted February 4, 2011 (edited) If $System_Log then PrintLogToTxt() $System_Log = "" EndIf $b = "1" For $i = 2 To 150000 $b &= "1" Next $y = 0 $timer = TimerInit() For $i = 1 To 1000000 if $b Then $y +=1 Next ConsoleWrite(TimerDiff($timer) & @tab & $y & @CRLF) $y = 0 $timer = TimerInit() For $i = 1 To 1000000 if NOT (StringLen($b) == 0) Then $y +=1 Next ConsoleWrite(TimerDiff($timer) & @tab & $y & @CRLF) $y = 0 $timer = TimerInit() For $i = 1 To 1000000 If NOT ($b == "") Then $y +=1 Next ConsoleWrite(TimerDiff($timer) & @tab & $y & @CRLF) $y = 0 $timer = TimerInit() For $i = 1 To 1000000 if $b <> "" Then $y +=1 Next ConsoleWrite(TimerDiff($timer) & @tab & $y & @CRLF) $a = 1 $b = 2 $timer = TimerInit() For $i = 0 To 1000000 If $a = $b Then Else EndIf Next ConsoleWrite(TimerDiff($timer) & @CRLF) $timer = TimerInit() For $i = 0 To 1000000 Select Case $a = $b Case Else EndSelect Next ConsoleWrite(TimerDiff($timer) & @CRLF) $timer = TimerInit() For $i = 0 To 1000000 Switch $a Case $b Case Else EndSwitch Next ConsoleWrite(TimerDiff($timer) & @CRLF) Edited February 4, 2011 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Xibalba Posted February 5, 2011 Author Share Posted February 5, 2011 Thanks for some input.(New) Question 3)If I have a lot of boolean variables which i check and/or set, which is faster:If $myBool Then $myBool = False EndIfor just simply set it every time (at this specific line):$myBool = FalseThis code runs a few hundred times per second, so it will matter in the long run.Thanks in advance Link to comment Share on other sites More sharing options...
Ascend4nt Posted February 5, 2011 Share Posted February 5, 2011 I've thought about optimization in the past with AutoIt, and basically came to the conclusion that the less text AutoIt has to parse, the better. The fastest - set $myBool to 0 each time. 'False' isn't really needed other than for text output. A setting of any value other than 0 can be interpreted as 'True'. My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
guinness Posted February 5, 2011 Share Posted February 5, 2011 I think the same might be said for the size of the AU3 file (or I could be wrong) If I have a single If...EndIf statement, I tend to have it on the one line. If $iInt = 0 Then ConsoleWrite("0" & @CRLF) Instead of... If $iInt = 0 Then ConsoleWrite("0" & @CRLF) EndIf UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Tvern Posted February 5, 2011 Share Posted February 5, 2011 I've thought about optimization in the past with AutoIt, and basically came to the conclusion that the less text AutoIt has to parse, the better. I've always found multiline If's to be faster than single line If's though. Doesn't stop me from using single line If's. To answer Q3: From what I can test it seems that evaluating this: If False Then ;this never gets executed $myBool = False EndIf Takes about twice as long as setting this, when $myBool is always false: $myBool = False In fact this: $myBool = False $myBool = True Is only marginally slower than evaluating the if statement above, even if the if statement never has to actually change any variables. So in no circumstance could it be faster to use the if statement. This changes when you are setting a lot of variables, or more complex variables, but for a bool, just set it every time. One interesting thing I read on the topic a while ago is that autoit takes longer to execute code with longer variable and function names. So you might want to replace it all for names with 1 to 3 characters depending on how many names you need. I think there might be an obfuscator option to do so. The ultimate performance optimization would probably to learn something besides AutoIt for these applications. (that's not going to make me popular now is it?) Link to comment Share on other sites More sharing options...
KaFu Posted February 5, 2011 Share Posted February 5, 2011 I think there might be an obfuscator option to do so.This is my standard option to boost performance ...#Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
whim Posted February 5, 2011 Share Posted February 5, 2011 @Xibalba You might also like Bowmore's AU3Profiler, search the Examples forum Link to comment Share on other sites More sharing options...
MvGulik Posted February 5, 2011 Share Posted February 5, 2011 (edited) whatever Edited February 7, 2011 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
Bowmore Posted February 5, 2011 Share Posted February 5, 2011 (edited) I've always found multi line IFs to be faster than single line IFs though. Doesn't stop me from using single line IFs. I'm of the opinion that the first priority should be that the code should be clear and the logic used easy to follow. Only when that has been achieved and the application is working as expected should we worry about speed and then concentrate on those areas which are the real bottle necks. Tvern I was interested in your comment regarding the speed of multi line IFs verses single line IFs, so I did a bit of testing. It appears that there is a significant difference in speed between the two forms. Strangely though the difference is reversed depending on whether the test expression evaluates true or false as shown in the tests below. In the tests labeled AT BT CT DT the expression evaluates to true. In the tests labeled AF BF CF DF the expression evaluates to false. expandcollapse popup$b = "qwertyuiytrew" ;evaluates to true $y = 0 $timer = TimerInit() For $i = 1 To 1000000 if $b <> "" Then $y +=1 Next ConsoleWrite("AT " & TimerDiff($timer) & @tab & $y & @CRLF) $y = 0 $timer = TimerInit() For $i=1 To 1000000 If $b<>"" Then $y+=1 EndIf Next ConsoleWrite("BT " & TimerDiff($timer) & @tab & $y & @CRLF) $y = 0 $timer = TimerInit() For $i = 1 To 1000000 if $b Then $y+=1 Next ConsoleWrite("CT " & TimerDiff($timer) & @tab & $y & @CRLF) $y = 0 $timer = TimerInit() For $i = 1 To 1000000 if $b Then $y+=1 EndIf Next ConsoleWrite("DT " & TimerDiff($timer) & @tab & $y & @CRLF) $b="" ;evaluates to false $y = 0 $timer = TimerInit() For $i = 1 To 1000000 if $b <> "" Then $y +=1 Next ConsoleWrite("AF " & TimerDiff($timer) & @tab & $y & @CRLF) $y = 0 $timer = TimerInit() For $i=1 To 1000000 If $b<>"" Then $y+=1 EndIf Next ConsoleWrite("BF " & TimerDiff($timer) & @tab & $y & @CRLF) $y = 0 $timer = TimerInit() For $i = 1 To 1000000 if $b Then $y+=1 Next ConsoleWrite("CF " & TimerDiff($timer) & @tab & $y & @CRLF) $y = 0 $timer = TimerInit() For $i = 1 To 1000000 if $b Then $y+=1 EndIf Next ConsoleWrite("DF " & TimerDiff($timer) & @tab & $y & @CRLF) Edit: Spelling Edited February 5, 2011 by Bowmore "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook Link to comment Share on other sites More sharing options...
MvGulik Posted February 5, 2011 Share Posted February 5, 2011 (edited) whatever Edited February 7, 2011 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
Ascend4nt Posted February 5, 2011 Share Posted February 5, 2011 (edited) Tvern & Bowmore, that's interesting regarding the multi--line If/EndIf's. I've tested Bowmore's code, removing all unneeded spaces and adding a sleep before execution of the main script (sometimes AutoIt needs 'initialization' time with a script), and this is the result: AT 2680.35570615504 1000000 BT 1900.54062567844 1000000 CT 1917.61630901653 1000000 DT 1120.32454568287 1000000 AF 1306.79400458893 0 BF 1542.82777028091 0 CF 547.604857968695 0 DF 803.950824347737 0 So on my PC, ~800 milliseconds extra for a matched single-line 'If..Then'. And ~250 ms. extra for non-matching multiline 'If..Then.EndIf' statements. Very interesting. I guess the scripter could determine which case is most likely to happen and then tweak the code based on that. Or just stick with 'If..Then..Endif', as that has the least performance impact either way. *edit: Thinking on this.. most of my code within loops where there is a single-line 'If..Then' usually results in an ExitLoop or Return, so those are best left alone.. Hmm.. and most people don't have anything running 1000000 times, so the scores above need to be taken with that in mind. Edited February 5, 2011 by Ascend4nt My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
Tvern Posted February 5, 2011 Share Posted February 5, 2011 Hmm.. and most people don't have anything running 1000000 times, so the scores above need to be taken with that in mind.Yeah I doubt this will have much practical use in most applications, but it's nice to have an idea of how these things behave. I was pretty surprised with the results I gor from Bowmore's script myself. Link to comment Share on other sites More sharing options...
Bowmore Posted February 5, 2011 Share Posted February 5, 2011 @Ascend4nt I agree it is easy to get carried away with small performance differences that don't have much impact in the real world. However personally I frequently use AutoIt at work to manipulate files with 25 to 50 million lines so small differences can somtimes make a significant time the application takes to run. Compared to using AutoIt in the first place is the biggest productivity boost. For many jobs, even if it is a one off the time taken to write a script and run it is far quicker than the alternative especially when you have a number of preprepared template scripts for different types of similar jobs. If you know something about the data you are processing significant performance increases can be achieved by ensuring the the most frequently occurring scenarios are checked for first in IF...Then; Switch or Select blocks. "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook Link to comment Share on other sites More sharing options...
MvGulik Posted February 6, 2011 Share Posted February 6, 2011 (edited) whatever Edited February 7, 2011 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now