|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Sat Oct 21, 2006 12:29 am
[1.10] Few Questions |
Just look for some feedback on my use of local variable and to ask a performance question.
My alias is conv_step which does an intermediate of money conversion by basically doing factorials.
I call it like conv_step 340 100 Crown
Here's the old version
Code: |
#VARIABLE StepResult ""
#MATH Temp1 %1/%2
#VARIABLE Remainder %mod( %1, %2) "" Money
#IF (@Temp1 > 0) {
#VARIABLE StepResult { @Temp1 %3} "" Money
#IF (@Temp1 > 2) {#VARIABLE StepResult %concat( @StepResult, "s")}
#VARIABLE StepResult %concat( @StepResult, ", ")
}
|
And the new version is
Code: |
#VAR StepResult "" Money
#local $Temp1 $ResultStr
$Temp1 = %1/%2
#VAR Remainder %mod( %1, %2) "" Money
#IF ($Temp1 > 0) {
$ResultStr = $Temp1 %3
#IF ($Temp1 > 1) { $ResultStr = $ResultStr + "s" }
$ResultStr = $ResultStr + ","
StepResult = $ResultStr
}
|
Also out of curiousity, which is faster? Calling the concat function like
Code: |
#VARIABLE StepResult %concat( @StepResult, ", ") |
or
Code: |
$ResultStr = $ResultStr + ",";
#VARIABLE StepResult $ResultStr |
The example is confusing but basical I wanted to know if doing a + "Random Strng" with a local variable is faster than calling the %concat function.
Finally for the line
Code: |
$ResultStr = $Temp1 %3 |
what's the best way to preserve the space also gets saved in the local variable. The most obvious way to me is
Code: |
$ResultStr = $Temp1 + " " + %3 |
but I was wondering if there was a better way.
Eventually I'll try playing around with named parameters as well. |
|
_________________ Asati di tempari! |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Sat Oct 21, 2006 1:47 am |
Any inline math functions, which includes using + for concat, will always be faster than calling a function. To call any function, CMUD has to load the arguments to the function onto the stack, then call the function, then get the result. Whereas doing + gets compiled directly as a CONCAT bytecode operation.
To answer questions like this you can usually learn a lot by looking at the compiled bytecode in the "Compiled Code" tab for your alias or trigger. In the case of %concat, you will see it pushing the arguments and then you'll see the FUNCREF bytecode to call the function. Whereas when you use + you will see the CONCAT bytecode directly.
To preserve your space, use a syntax like this:
$ResultStr = {$Temp1 %3}
Remember that {} acts like quotes except it allows variable expansion inside. But since it's acting like quotes, it preserves spaces. It's sort of like the difference between " and ' quotes in PHP if you have ever programmed in that language. |
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Sat Oct 21, 2006 1:48 am |
I think I think I tried the { } and i got some weird parsing issue.. but it might be the multiline thing kicking in.
|
|
_________________ Asati di tempari! |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Sat Oct 21, 2006 2:06 am |
There is also a bug with the varname={value} syntax when used within another statement (like #IF or #LOOP, etc) in v1.10 that is fixed in v1.11.
|
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Sat Oct 21, 2006 9:10 am |
Nice I'll give it a shot now.
|
|
_________________ Asati di tempari! |
|
|
|
|
|