Script execution time and timerdiff doesnt match
#1
Posted 27 February 2012 - 12:54 PM
Evereything works great. There is just one problem. The script takes a bit long to execute for me to satistfied sp i used the functions timerInit and TimerDiff to count the exact time the script takes to execute.
The problem is that it doesnt match the Internal timer.
The Timefiff function say 1.6 sec but the editor say 6,050 sec. How can that be?
The timerinit is the first thing that happends (even befor the includes) and the timerdiff is the last thing that happends.
Anyone has any idees?
#2
Posted 27 February 2012 - 01:03 PM
• AutoIt Version : 3.3.8.1/3.3.9.4 SciTE 3.3.0 Language:040C OS:WIN_7/ CPU:X64 OS:X64
- [Music] [2012-04-07] TinyChartsJukebox • [2012-07-28] TinyMp3Player • [2012-10-07] Mp3SearchEngine [2013-04-04] TinyMp3Downloader • [2012-11-05] TinyShoutCastTuner • [2012-11-01] BeepSongEditor
- [Upload] [2011-12-31] TinyFreeUploader • [2012-07-13] TinyPicCaptureUploader
- [Script Tools] [2012-10-03] SciTE Hopper • [2013-04-10] AutoIt&SciTEUpdater • [2012-12-21] TinyColorCatcher [2012-07-13] RegJumper • [2013-05-17] BinaryToAu3Kompressor • [2012-06-06] TinyAu3FilesSearch
- [Utilities] [2012-06-30] GetCommandLineParameters • [2011-12-21] TinyBackupBox • [2010-09-25] TinyBatteryBar [2011-12-05] TinyBrightnessControler • [2012-09-03] TinyUninstaller • [2012-07-01] TinyClipToSpeech
- [Diverse] [2013-03-06] AnimatedSplash • [2013-01-18] GuiCtrlPicButton Simulate Reaction • [2012-04-15] CanYouRunIt
• Last updated Scripts and executables with full embedded files are available on : GoogleCode
#3
Posted 27 February 2012 - 01:19 PM
The #include directives are always processed first.... even before the includes ...
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
#4
Posted 27 February 2012 - 02:20 PM
| (do stuff related to running the user source code.)
|
| o Start user code.
| |
| o-> user code exit time.
|
| (do some other stuff.)
o-> Editor time.
Edited by MvGulik, 27 February 2012 - 02:22 PM.
#5
Posted 27 February 2012 - 03:19 PM
#include <OutlookEx.au3> #include <ad.au3> #include <array.au3> $begin = TimerInit() _AD_Open() $oOutlook = _OL_Open() $file = FileOpen("undantag.csv", 0) if @OSLang = "041d" Then $signaturedir = @AppDataDir & "MicrosoftSignaturer" Else $signaturedir = @AppDataDir & "MicrosoftSignatures" EndIf dim $txt_lines[1], $nuvarande_signaturer[1] dim $standard = 1 ;~ Spara allt från csvfilen till en array While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop _ArrayAdd($txt_lines,$line) Wend _ArrayDelete($txt_lines,1) ;~ Stega igenom arrayen rad för rad och kör det som passar användaren som kör For $i = 1 to UBound($txt_lines)-1 Step 1 global $temp_split = stringsplit($txt_lines[$i],";") if $temp_split[1] = @username Then if $temp_split[2] = "extra" Then _readad($temp_split[3]) ConsoleWrite($mail & @CRLF) _skapasignatur($temp_split[3]) ElseIf $temp_split[2] = "special" Then $standard = 0 _readad(@username) ConsoleWrite($mail & @CRLF) _skapasignatur($temp_split[3]) EndIf EndIf Next ;~ Kör personens egna signatur if $standard = 1 then global $temp_split = stringsplit("hej;hej;hej;hej",";") _readad(@username) ConsoleWrite($mail & @CRLF) _skapasignatur(@username) EndIf $searchstring = _ArrayToString($nuvarande_signaturer,";",1) $search = FileFindFirstFile($signaturedir & "*.htm") While 1 $sokfil = FileFindNextFile($search) If @error Then ExitLoop $sokfil = StringReplace($sokfil,".htm","") $sokfil = StringReplace($sokfil," svar","") if StringInStr($searchstring,$sokfil) then Else ;~ msgbox(0,"",$sokfil & " hittade inte") DirRemove($signaturedir & $sokfil & "-filer",1) DirRemove($signaturedir & $sokfil & "-files",1) DirRemove($signaturedir & $sokfil & " svar-filer",1) DirRemove($signaturedir & $sokfil & " svar-files",1) FileDelete($signaturedir & $sokfil & ".htm") FileDelete($signaturedir & $sokfil & ".rtf") FileDelete($signaturedir & $sokfil & ".txt") FileDelete($signaturedir & $sokfil & " svar.htm") FileDelete($signaturedir & $sokfil & " svar.rtf") FileDelete($signaturedir & $sokfil & " svar.txt") EndIf WEnd global $displayname = _AD_GetObjectAttribute(@UserName,"displayName") _OL_MailSignatureSet($displayname,$displayname & " svar") _OL_Close($oOutlook) _AD_Close() FileClose($search) FileClose($file) consolewrite("Allt: " & TimerDiff($begin)/1000 & @CRLF) Exit ;~ Funktioner func _readad($AD_user) global $displayname = _AD_GetObjectAttribute($AD_user,"displayName") global $mail = _AD_GetObjectAttribute($AD_user,"mail") global $skype = _AD_GetObjectAttribute($AD_user,"ipPhone") global $tele = _AD_GetObjectAttribute($AD_user,"telephoneNumber") global $titel = _AD_GetObjectAttribute($AD_user,"title") global $street = _AD_GetObjectAttribute($AD_user,"streetAddress") global $postnr = _AD_GetObjectAttribute($AD_user,"postalCode") global $ort = _AD_GetObjectAttribute($AD_user,"l") _ArrayAdd($nuvarande_signaturer,$displayname) EndFunc func _skapasignatur($user) $uppdatera_main = 1 $uppdatera_svar = 1 $array1 = StringSplit($mail,"@") if $uppdatera_main = 1 Then Local $oOL_Word = ObjCreate("Word.Application") Local $oOL_Doc = $oOL_Word.Documents.Add() Local $oOL_Selection = $oOL_Word.Selection $oOL_Selection.TypeText("Text of test Signature") $oOL_Selection = $oOL_Doc.Range() $iResult = _OL_MailSignatureCreate($displayname, $oOL_Word, $oOL_Selection, False, False) if $street = "" then $street = "Slussplan 9" if $postnr <> "" and $ort <> "" Then $stad = $postnr & " " & $ort Else $stad = "Box 38065 | SE-100 64 STOCKHOLM" EndIf if $temp_split[2] = "special" Then $file_html_template = FileOpen(@ScriptDir & "special" & $user & ".htm",0) Else $file_html_template = FileOpen(@ScriptDir & "" & $array1[2] & ".htm",0) EndIf $HTML = FileRead($file_html_template) if StringInStr($stad,"stockholm") then $reklam_text = FileRead(@ScriptDir & "disc_sv.txt") if $postnr <> "" and $ort <> "" Then $reklam_text = FileRead(@ScriptDir & "disc_en.txt") $HTML = StringReplace($HTML,"#DisplayName#",$displayname) $HTML = StringReplace($HTML,"#email#",$mail) $HTML = StringReplace($HTML,"<p>reklam.txt</p>",$reklam_text) $HTML = StringReplace($HTML,"#street#",$street) $HTML = StringReplace($HTML,"#stad#",$stad) if $skype <> "" then $HTML = StringReplace($HTML,"#skype#",$skype & " | Skype<BR>") Else $HTML = StringReplace($HTML,"#skype#","") EndIf if $tele <> "" then $HTML = StringReplace($HTML,"#tele#",$tele & " | direct<BR>") Else $HTML = StringReplace($HTML,"#tele#","") EndIf if $titel <> "" then $HTML = StringReplace($HTML,"#titel#","<br>" & $titel) Else $HTML = StringReplace($HTML,"#titel#","") EndIf if $titel <> "" then $HTML = StringReplace($HTML,"#titel#","<br>" & $titel) Else $HTML = StringReplace($HTML,"#titel#","") EndIf FileDelete($signaturedir & $displayname & ".htm") FileWrite($signaturedir & $displayname & ".htm",$HTML) FileClose($file_html_template) EndIf if $uppdatera_svar = 1 Then Local $oOL_Word = ObjCreate("Word.Application") Local $oOL_Doc = $oOL_Word.Documents.Add() Local $oOL_Selection = $oOL_Word.Selection $oOL_Selection.TypeText("Text of test Signature") $oOL_Selection = $oOL_Doc.Range() $iResult = _OL_MailSignatureCreate($displayname & " svar", $oOL_Word, $oOL_Selection, False, False) if $temp_split[2] = "special" Then $file_html_template = FileOpen(@ScriptDir & "special" & $user & " svar.htm",0) Else $file_html_template = FileOpen(@ScriptDir & "" & $array1[2] & " svar.htm",0) EndIf $HTML = FileRead($file_html_template) $HTML = StringReplace($HTML,"#DisplayName#",$displayname) $HTML = StringReplace($HTML,"#tele#",$tele) FileDelete($signaturedir & $displayname & " svar.htm") FileWrite($signaturedir & $displayname & " svar.htm",$HTML) FileClose($file_html_template) EndIf EndFunc
I know that its not the best code and i know that i can probably speed things up using regular expressions. But to be honest, im still a newbie and I havent learned how to use regular expressions.
The thing is that when i execute the script, the lase consolewrite gives me the time about 2sec after i run the script. But it keeps running for another 4sec after that befor completing. So whats running for 4 secs after the "Exit" commnad?
#6
Posted 27 February 2012 - 03:31 PM
How to ask questions the smart way!
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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.
GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.
#7
Posted 27 February 2012 - 03:42 PM
#8
Posted 27 February 2012 - 03:43 PM
TimerDiff: 0.00670476275616035
+>16:39:40 AutoIT3.exe ended.rc:0
>Exit code: 0 Time: 1.620
May be your SciTE lagg a bit...
• AutoIt Version : 3.3.8.1/3.3.9.4 SciTE 3.3.0 Language:040C OS:WIN_7/ CPU:X64 OS:X64
- [Music] [2012-04-07] TinyChartsJukebox • [2012-07-28] TinyMp3Player • [2012-10-07] Mp3SearchEngine [2013-04-04] TinyMp3Downloader • [2012-11-05] TinyShoutCastTuner • [2012-11-01] BeepSongEditor
- [Upload] [2011-12-31] TinyFreeUploader • [2012-07-13] TinyPicCaptureUploader
- [Script Tools] [2012-10-03] SciTE Hopper • [2013-04-10] AutoIt&SciTEUpdater • [2012-12-21] TinyColorCatcher [2012-07-13] RegJumper • [2013-05-17] BinaryToAu3Kompressor • [2012-06-06] TinyAu3FilesSearch
- [Utilities] [2012-06-30] GetCommandLineParameters • [2011-12-21] TinyBackupBox • [2010-09-25] TinyBatteryBar [2011-12-05] TinyBrightnessControler • [2012-09-03] TinyUninstaller • [2012-07-01] TinyClipToSpeech
- [Diverse] [2013-03-06] AnimatedSplash • [2013-01-18] GuiCtrlPicButton Simulate Reaction • [2012-04-15] CanYouRunIt
• Last updated Scripts and executables with full embedded files are available on : GoogleCode
#9
Posted 27 February 2012 - 03:43 PM
Quite a lot actually. Not a Dev, but I do know that AutoIt does a fair amount of housekeeping to clear up after poorly coded scripts - closing GUIs, handles, processes, etc. I can quite imagine that 2 secs is needed to make sure everything is indeed closed down after what might well be a forced termination.So whats running for 4 secs after the "Exit" commnad?
If you are really curious about the details, post a question in the "Developer Chat" section and see if you can catch a Dev on a good day.
M23
Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs
Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames
GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes
ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display
RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options
GUIListViewEx - Insert, delete, move, drag and sort ListView items
#10
Posted 27 February 2012 - 04:18 PM
I just want my script to run as fast as possibel and try to learn somthing to make a better script next time. Thas all
#11
Posted 27 February 2012 - 08:29 PM
This leads me to believe that you are willing to accept some coding advice.
Some general observations:I just want my script to run as fast as possibel and try to learn somthing to make a better script next time.
Do not define variables in a loop
Do NOT use _arrayadd in a filereadline loop as redim is high overhead
Check return codes from functions and add meaningfull messages for failures
Watch how you are using GLOBAL when defining variables. Avoid using GLOBAL where possible.
Define script wide (GLOBAL) variable near the top of the script
I have no experience with the Outlook UDF so cannot comment on that code. The following code contains some of the changes that I think will improve this code. (untested)
#include <OutlookEx.au3> #include <ad.au3> #include <array.au3> $begin = TimerInit() _AD_Open() $oOutlook = _OL_Open() ; always check return codes $file = FileOpen("undantag.csv", 0) If $file = -1 Then MsgBox(0,'Open failed','Open for read for file = ... failed') ;;;;; if @OSLang = "041d" Then $signaturedir = @AppDataDir & "MicrosoftSignaturer" Else $signaturedir = @AppDataDir & "MicrosoftSignatures" EndIf ;dim $txt_lines[1], $nuvarande_signaturer[1] removed definition for $txt_lines Dim $nuvarande_signaturer[1] dim $standard = 1 ; populating your array in a read loop using _arrayadd is high overhead as a redim is done for each record read ; instead, populate you array like this: ;~ Spara allt från csvfilen till en array ;~ While 1 ;~ $line = FileReadLine($file) ;~ If @error = -1 Then ExitLoop ;~ _ArrayAdd($txt_lines,$line) ;~ Wend ;~ _ArrayDelete($txt_lines,1) Local $tmp_file = FileRead($file) If @error = 1 Then MsgBox(0,'Error reading file for array population','') ; if you are running this using SCITE then most people use "consolewrite" $txt_lines = StringSplit($tmp_file,@crlf,3) ; see help file for parameters ; this could also be condensed to this: $txt_lines = StringSplit(FileRead($file),@crlf,3) ; however, you cannot check the results from fileread directly FileClose($file) ; this aids in cleanup at script end and is good coding practice $file = 0 ;;; ;~ Stega igenom arrayen rad för rad och kör det som passar användaren som kör For $i = 1 to UBound($txt_lines)-1 Step 1 ;global $temp_split = stringsplit($txt_lines[$i],";") $temp_split = stringsplit($txt_lines[$i],";") ; do NOT define variable in a loop if $temp_split[1] = @username Then if $temp_split[2] = "extra" Then _readad($temp_split[3]) ConsoleWrite($mail & @CRLF) _skapasignatur($temp_split[3]) ElseIf $temp_split[2] = "special" Then $standard = 0 _readad(@username) ConsoleWrite($mail & @CRLF) _skapasignatur($temp_split[3]) EndIf EndIf Next ;~ Kör personens egna signatur if $standard = 1 then ;global $temp_split = stringsplit("hej;hej;hej;hej",";") ; this variable is already defined, no need to define it again $temp_split = stringsplit("hej;hej;hej;hej",";") ; does this make sense? it results in a 4 element array of "hej" entries _readad(@username) ; this uses _arrayadd but is OK as it occurs only once ConsoleWrite($mail & @CRLF) _skapasignatur(@username) EndIf $searchstring = _ArrayToString($nuvarande_signaturer,";",1) $search = FileFindFirstFile($signaturedir & "*.htm") While 1 $sokfil = FileFindNextFile($search) If @error Then ExitLoop $sokfil = StringReplace($sokfil,".htm","") $sokfil = StringReplace($sokfil," svar","") if StringInStr($searchstring,$sokfil) then Else ;~ msgbox(0,"",$sokfil & " hittade inte") DirRemove($signaturedir & $sokfil & "-filer",1) DirRemove($signaturedir & $sokfil & "-files",1) DirRemove($signaturedir & $sokfil & " svar-filer",1) DirRemove($signaturedir & $sokfil & " svar-files",1) FileDelete($signaturedir & $sokfil & ".htm") FileDelete($signaturedir & $sokfil & ".rtf") FileDelete($signaturedir & $sokfil & ".txt") FileDelete($signaturedir & $sokfil & " svar.htm") FileDelete($signaturedir & $sokfil & " svar.rtf") FileDelete($signaturedir & $sokfil & " svar.txt") EndIf WEnd global $displayname = _AD_GetObjectAttribute(@UserName,"displayName") _OL_MailSignatureSet($displayname,$displayname & " svar") _OL_Close($oOutlook) _AD_Close() FileClose($search) FileClose($file) consolewrite("Allt: " & TimerDiff($begin)/1000 & @CRLF) Exit ;~ Funktioner func _readad($AD_user) global $displayname = _AD_GetObjectAttribute($AD_user,"displayName") global $mail = _AD_GetObjectAttribute($AD_user,"mail") global $skype = _AD_GetObjectAttribute($AD_user,"ipPhone") global $tele = _AD_GetObjectAttribute($AD_user,"telephoneNumber") global $titel = _AD_GetObjectAttribute($AD_user,"title") global $street = _AD_GetObjectAttribute($AD_user,"streetAddress") global $postnr = _AD_GetObjectAttribute($AD_user,"postalCode") global $ort = _AD_GetObjectAttribute($AD_user,"l") _ArrayAdd($nuvarande_signaturer,$displayname) EndFunc func _skapasignatur($user) $uppdatera_main = 1 $uppdatera_svar = 1 $array1 = StringSplit($mail,"@") if $uppdatera_main = 1 Then Local $oOL_Word = ObjCreate("Word.Application") Local $oOL_Doc = $oOL_Word.Documents.Add() Local $oOL_Selection = $oOL_Word.Selection $oOL_Selection.TypeText("Text of test Signature") $oOL_Selection = $oOL_Doc.Range() $iResult = _OL_MailSignatureCreate($displayname, $oOL_Word, $oOL_Selection, False, False) if $street = "" then $street = "Slussplan 9" if $postnr <> "" and $ort <> "" Then $stad = $postnr & " " & $ort Else $stad = "Box 38065 | SE-100 64 STOCKHOLM" EndIf if $temp_split[2] = "special" Then $file_html_template = FileOpen(@ScriptDir & "special" & $user & ".htm",0) Else $file_html_template = FileOpen(@ScriptDir & "" & $array1[2] & ".htm",0) EndIf $HTML = FileRead($file_html_template) if StringInStr($stad,"stockholm") then $reklam_text = FileRead(@ScriptDir & "disc_sv.txt") if $postnr <> "" and $ort <> "" Then $reklam_text = FileRead(@ScriptDir & "disc_en.txt") $HTML = StringReplace($HTML,"#DisplayName#",$displayname) $HTML = StringReplace($HTML,"#email#",$mail) $HTML = StringReplace($HTML,"<p>reklam.txt</p>",$reklam_text) $HTML = StringReplace($HTML,"#street#",$street) $HTML = StringReplace($HTML,"#stad#",$stad) if $skype <> "" then $HTML = StringReplace($HTML,"#skype#",$skype & " | Skype<BR>") Else $HTML = StringReplace($HTML,"#skype#","") EndIf if $tele <> "" then $HTML = StringReplace($HTML,"#tele#",$tele & " | direct<BR>") Else $HTML = StringReplace($HTML,"#tele#","") EndIf if $titel <> "" then $HTML = StringReplace($HTML,"#titel#","<br>" & $titel) Else $HTML = StringReplace($HTML,"#titel#","") EndIf if $titel <> "" then $HTML = StringReplace($HTML,"#titel#","<br>" & $titel) Else $HTML = StringReplace($HTML,"#titel#","") EndIf FileDelete($signaturedir & $displayname & ".htm") FileWrite($signaturedir & $displayname & ".htm",$HTML) FileClose($file_html_template) EndIf if $uppdatera_svar = 1 Then Local $oOL_Word = ObjCreate("Word.Application") Local $oOL_Doc = $oOL_Word.Documents.Add() Local $oOL_Selection = $oOL_Word.Selection $oOL_Selection.TypeText("Text of test Signature") $oOL_Selection = $oOL_Doc.Range() $iResult = _OL_MailSignatureCreate($displayname & " svar", $oOL_Word, $oOL_Selection, False, False) if $temp_split[2] = "special" Then $file_html_template = FileOpen(@ScriptDir & "special" & $user & " svar.htm",0) Else $file_html_template = FileOpen(@ScriptDir & "" & $array1[2] & " svar.htm",0) EndIf $HTML = FileRead($file_html_template) $HTML = StringReplace($HTML,"#DisplayName#",$displayname) $HTML = StringReplace($HTML,"#tele#",$tele) FileDelete($signaturedir & $displayname & " svar.htm") FileWrite($signaturedir & $displayname & " svar.htm",$HTML) FileClose($file_html_template) EndIf EndFunc
Good Luck,
kylomas
#12
Posted 28 February 2012 - 08:54 AM
I will deffentenly look at your suggestions and rewrite some of the code.
Just a few things.
* I cant use any msgbox code becuase this program will be running on severeal client machines, and the idee is that none is suppsoed to know that its there.
* The reason for my "global" variables inside the code, is becuase i made part of this script as seperate programs and just to make it work i needed to declare some varibales as global. I should clean up the code i know
* I knolw that _arrayadd in a fileread loop is not the best way of doing it. I just thought that a txtfile with 1-20 rows, would not matter that mutch. Any siggestions on how to do it instead? Perhaps I could replace the for loop (for the array) with the while loop (the one that reads all the lines).
Just an edit for the $temp_split = stringsplit("hej;hej;hej;hej",";") haha
I was going to demo the script to my boss, and when i did, i got errors becuase the file was empry (i can be). So just to populate the array $temp_split with... somthing. I just quickly wrote that line.
It shouldt be there
One more edit.
So its best practice to deffine all variabels at the start of the script.
Edited by Tjalve, 28 February 2012 - 09:26 AM.
#13
Posted 28 February 2012 - 12:45 PM
See the modified code that I posted in #11 for some quick alternatives to filling an array from a file...
Good Luck,
kylomas
#14
Posted 28 February 2012 - 02:39 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users






