| View previous topic :: View next topic |
| Author |
Message |
niwi
Joined: 27 Feb 2005 Posts: 128 Location: Heidelberg, Germany
|
Posted: Sat Mar 05, 2005 11:54 am Post subject: Expressions |
|
|
Hi,
I have a new question:
| Code: | vText = ABC
vNum = 1
If vText = ABC
{
If vNum = 1
{
MsgBox, first
}
}
If (vText = ABC AND vNum = 1)
{
MsgBox, second
}
|
Why does this script do not display "second"? Is it not possible to combine string expressions with numeric?
Or is this made by desgin?
Regards, NiWi. |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Sat Mar 05, 2005 11:56 am Post subject: |
|
|
| You need quotes around "ABC". Literal strings in expressions must be in quotes. The reason that no error is displayed is that ABC is assumed to be a variable (or an environment variable). |
|
| Back to top |
|
 |
niwi
Joined: 27 Feb 2005 Posts: 128 Location: Heidelberg, Germany
|
Posted: Sat Mar 05, 2005 12:31 pm Post subject: |
|
|
Hi Chris,
| Chris wrote: | | You need quotes around "ABC". Literal strings in expressions must be in quotes. The reason that no error is displayed is that ABC is assumed to be a variable (or an environment variable). |
That it is.
Thank you, I hope, my code will now be get a little bit smaller...
NiWi.
BTW: Is there a difference in performance using the first or the second method? |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Sun Mar 06, 2005 3:50 am Post subject: |
|
|
| niwi wrote: | | Is there a difference in performance using the first or the second method? | There is probably a difference but I wouldn't expect one to be more than twice as fast as the other. Offhand, I don't know which is faster.
Here is an example of how to write a benchmark script to find out which syntax methods are faster: | Code: | SetBatchLines -1
StartTime := A_TickCount
Loop, 200000
if Var = 1
MsgBox This is never shown.
Elapsed1 := A_TickCount - StartTime
MsgBox %Elapsed1% |
|
|
| Back to top |
|
 |
niwi
Joined: 27 Feb 2005 Posts: 128 Location: Heidelberg, Germany
|
Posted: Sun Mar 06, 2005 3:49 pm Post subject: |
|
|
Hi, I wrote a little benchmark based on Chris's one. Maybe some people are interested in the results. It seems to be better to split If expressions into single if-cases. If you know something about the chances for each case it seems to be good to check the worst first, so you will just have a few left for the second check and so on.
| Code: | SetBatchLines -1
Var1 := 0
MaxCount := 20000000
fiftyPercent := MaxCount / 2
StartTime := A_TickCount
Loop, %MaxCount%
{
if a_Index > %fiftyPercent%
{
if Var1 = 0
Var2 := A_Index
}
}
EndTime := A_TickCount
Elapsed1 := (EndTime - StartTime) / 1000
StartTime := A_TickCount
Loop, %MaxCount%
{
if Var1 = 0
{
if a_Index > %fiftyPercent%
Var2 := A_Index
}
}
EndTime := A_TickCount
Elapsed2 := (EndTime - StartTime) / 1000
StartTime := A_TickCount
Loop, %MaxCount%
{
if (a_Index > fiftyPercent) AND (Var1 = 0)
{
Var2 := A_Index
}
}
EndTime := A_TickCount
Elapsed3 := (EndTime - StartTime) / 1000
StartTime := A_TickCount
Loop, %MaxCount%
{
if (Var1 = 0) AND (a_Index > fiftyPercent)
{
Var2 := A_Index
}
}
EndTime := A_TickCount
Elapsed4 := (EndTime - StartTime) / 1000
SetFormat, float, 0.2
Transform, Elapsed1, Round, %Elapsed1%, 2
Transform, Elapsed2, Round, %Elapsed2%, 2
Transform, Elapsed3, Round, %Elapsed3%, 2
Transform, Elapsed4, Round, %Elapsed4%, 2
MsgBox Loop1: %Elapsed1% s`nLoop2: %Elapsed2% s`nLoop3: %Elapsed3% s`nLoop4: %Elapsed4% s
; Result:
; MaxCount := 20000000
; Loop1: 88.60 s
; Loop2: 93.50 s
; Loop3: 125.27 s
; Loop4: 127.85 s |
|
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Sun Mar 06, 2005 10:15 pm Post subject: |
|
|
Your results at the bottom are quite interesting, especially that expressions are a little slower than separate IF statements if you order the IFs so that the one more likely to be "false" is listed first (which prevents the execution of the second IF most of the time).
Although expressions are slower in cases like these, it's a relief that they aren't dramatically slower.
I'm planning to implement short-circuit boolean evaluation in conjunction with function calling. In addition to avoiding unnecessary function calls, this should speed up your test script because the short-circuit would prevent the second half of an "AND" from being evaluated if the first half evaluates to "false". |
|
| Back to top |
|
 |
Andi
Joined: 11 Feb 2005 Posts: 153 Location: Germany, Niestetal
|
Posted: Mon Mar 07, 2005 12:17 am Post subject: |
|
|
Does someone has an explanation why the following script works
... with a %A_Space% before ABC instead "ABC"...
| Code: |
vText = ABC
vNum = 1
If vText = ABC
{
If vNum = 1
{
MsgBox, first
}
}
If (vText = %A_space%ABC) AND (vNum = 1)
{
MsgBox, second
}
|
... and this one not?
| Code: |
vText = ABC
vNum = 1
If vText = ABC
{
If vNum = 1
{
MsgBox, first
}
}
If (vText = %A_space%ABC AND vNum = 1)
{
MsgBox, second
}
|
|
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Mon Mar 07, 2005 4:04 pm Post subject: |
|
|
| Andi wrote: | why the following script works ... with a %A_Space% before ABC instead "ABC"
...
If (vText = %A_space%ABC) AND (vNum = 1)
... and this one not?
If (vText = %A_space%ABC AND vNum = 1) | Variables containing spaces, such as A_Space, should not be flush up against the name of another variable. This is because the it would produce spaces in the resulting dynamically-built variable name, which is not allowed.
Although the behavior you illustrated is inconsistent and strange, I don't see an easy way to fix it yet. But thanks for pointing it out. |
|
| Back to top |
|
 |
Tekl
Joined: 24 Sep 2004 Posts: 813 Location: Germany
|
Posted: Sun Mar 13, 2005 3:52 am Post subject: |
|
|
Hi,
what's wrong with this expression?
| Code: | | Menu, wr_Edit, Add, % "Edit: " wr_Name[%A_Index%], wr_EditEntry |
I'll get only "Edit:". Without "Edit: " I'll get the variable.
Tekl |
|
| Back to top |
|
 |
niwi
Joined: 27 Feb 2005 Posts: 128 Location: Heidelberg, Germany
|
Posted: Sun Mar 13, 2005 11:41 am Post subject: |
|
|
Hi Tekl,
Have you tried something like this:
| Code: |
dummy := "Edit: " wr_Name[%A_Index%]
Menu, wr_Edit, Add, %dummy% , wr_EditEntry
|
I'm still a little bit confused using expressions and getting the expected results
NiWi. |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Sun Mar 13, 2005 5:29 pm Post subject: |
|
|
Currently, expressions cannot concatenate array elements. This is a documented limitation, but hopefully will be overcome in the near future.
To work around this, do it in two steps:
element := wr_Name[%A_Index%]
Menu, wr_Edit, Add, % "Edit: " element, wr_EditEntry |
|
| Back to top |
|
 |
Tekl
Joined: 24 Sep 2004 Posts: 813 Location: Germany
|
Posted: Sun Mar 13, 2005 6:52 pm Post subject: |
|
|
Sorry, I haven't read this. Maybe there could be some more examples in the help and examples for thing they are not possible at the moment.
Tekl |
|
| Back to top |
|
 |
niwi
Joined: 27 Feb 2005 Posts: 128 Location: Heidelberg, Germany
|
Posted: Sun Mar 13, 2005 7:11 pm Post subject: |
|
|
Hi,
| Tekl wrote: | | Sorry, I haven't read this. Maybe there could be some more examples in the help and examples for thing they are not possible at the moment. |
This is a good idea. Also it would be nice to have a section with the todo list.
NiWi. |
|
| Back to top |
|
 |
Tekl
Joined: 24 Sep 2004 Posts: 813 Location: Germany
|
|
| Back to top |
|
 |
|