@Blue Bear
No I haven't looked into those yet. I will do after I tried Tigerlily's solution!
@Tigerlily
Oh man, that's great! It's doing exactly what I want!
I implemented your code in my script and it works perfectly! If someone is interested I posted my script below. The script measures the time it takes to apply a value to a variable inside if-statements and outside.
I need to do this because I'm building a script with a lot of if's and need to test if all the if-statements have a big influence on the run time (conlusion=they don't).
Below I've made 3 'functions' that assign values to a variable with and without the help of the if-statements. It runs the 3 functions 1000 times (could be changed) to get a reliable outcome which I'm able to use to prove that if-statements have (not) an influence on script run time.
The runtime of each 'assigning-value-to-variable-function' gets pasted in Excel immediately.
Code: Select all
x = 1
z = 2
Xl := ComObjActive("Excel.Application")
Xl.Range("A1").Select
loop 1000 {
Speed1()
Trigger = 0
Loop 1000000
{
y = a
}
Speed2Msgbox("Just assigning a value to a variable 1 million times took ")
Speed1()
Trigger = 0
Loop 1000000
{
If (x = 1)
y = a
}
Speed2Msgbox("Testing a variable against a value and then assigning a value, each 1 million times, took ")
Speed1()
Trigger = 1
Loop 1000000
{
If (x = 1){
}
If (x = 2)
if (x != 3)
if(z = 2)
y = a+=2
}
Speed2Msgbox("Testing against a value twice and then assigning a value, each 1 million times, took ")
}
Return
;----Functions to measure performance/speed----
Speed1() {
global
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
}
Speed2() {
global
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
}
Speed2Msgbox(MessageBegin = "The operation took ") {
global
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
DllCall("QueryPerformanceFrequency", "Int64*", Frequency)
TimeMS := (CounterAfter - CounterBefore) / (Frequency / 1000)
If WinActive("ahk_exe excel.exe") {
; global removed this because the variable is declared inside the function? got an error when I kept it in
Xl.Selection.Value := TimeMS
if (Trigger!=1){
Xl.Selection.Offset(1,0).Select
} else {
Xl.Selection.Offset(-2,1).Select
}
} else {
WinActivate, ahk_exe excel.exe
Xl.Selection.Offset(1,0).Select
}
}
return
The script runtime function is written by Cerberus and I modified it with your help so it pasts the runtime directly in Excel. I know nothing about
DllCall so I couldn't change much about the setup. I'm glad it works now.
Only one thing tho, which isn't a problem really but just for the sake of it...
Cell 'A1' remains empty atm, which is obvious bcs cell 'A1' gets selected and
Xl.Selection.Offset(1,0).Select this gets ran immediately after. As I already mentioned, it doesn't matter bcs the runtime gets taken 999 times instead of 1000 but is there a way to ensure that cell 'A1' gets also filled in?
Thanks again, love it!