Jump to content

Script execution time and timerdiff doesnt match


Tjalve
 Share

Recommended Posts

I have created a script that creates a Outlook signature based on AD and a template.

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?

Link to comment
Share on other sites

... even before the includes ...

The #include directives are always processed first.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

o Run my script/Editor timer.

| (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

"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

Alright. here is my code.

#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 & " |&nbsp;Skype<BR>")
  Else
   $HTML = StringReplace($HTML,"#skype#","")
  EndIf
 
  if $tele <> "" then
   $HTML = StringReplace($HTML,"#tele#",$tele & " |&nbsp;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?

Link to comment
Share on other sites

I'm not sure what's being done, only the Developers would know for sure, but even a one line consolewrite takes over 2 seconds from start to the time SciTe says it's done. I wouldn't worry too much about it, 2-4 seconds shouldn't be that much of an issue.

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

  • Moderators

Tjalve,

So whats running for 4 secs after the "Exit" commnad?

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. :oops:

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. :bye:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Tjalve,

This leads me to believe that you are willing to accept some coding advice.

I just want my script to run as fast as possibel and try to learn somthing to make a better script next time.

Some general observations:

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

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thank you kylomas. As i said befor. I have never read any programing in school or books. I just did a littel trial and error a few weeks back and leardned on the way.

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 :oops:

* 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 :bye:

One more edit.

So its best practice to deffine all variabels at the start of the script.

Edited by Tjalve
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...