Thank you again! Updated
but remember about performance ...
ClassMethod Test(val = 3) [ ProcedureBlock = 0 ]{
s:'$d(^||addr) ^||addr(1)=1, (^(2),^(8),^(9))=289, ^(3)=3
d @("t1case" _ $g( ^||addr( val ) ) ) Q
t1case1 w !, "Case 1" Q
t1case3 w !, "Case 3" Q
t1case289 w !, "Case 2 or 8 or 9" Q
t1case w !,"Case default" Q
}
/// Local variable with indirection
ClassMethod Test2(val = 3) [ ProcedureBlock = 0 ]{
s:'$d(addr) addr(1)=1, (addr(2),addr(8),addr(9))=289, addr(3)=3
d @( "t2case"_$g( addr( val ) ) ) Q
t2case1 w !, "Case 1" Q
t2case3 w !, "Case 3" Q
t2case289 w !, "Case 2 or 8 or 9" Q
t2case w !,"Case default" Q
}
/// Without indirection
ClassMethod Test3(val = 3) [ ProcedureBlock = 0 ]{
if val=1 w !, "Case 1" Q
if val=3 w !, "Case 3" Q
if (val=2)!(val=8)!(val=9) w !, "Case 2 or 8 or 9" Q
w !,"Case default"
}
/// Process private global without indirection
ClassMethod Test4(val = 3) [ ProcedureBlock = 0 ]{
s:'$d(^||addr) ^||addr(1)=1, (^(2),^(8),^(9))=289, ^(3)=3
d $case($g(^||addr(val)),1:t4case1,289:t4case289,3:t4case3,:t4case)
Q
t4case1 w !, "Case 1" Q
t4case3 w !, "Case 3" Q
t4case289 w !, "Case 2 or 8 or 9" Q
t4case w !,"Case default" Q
}
/// Local variable without indirection
ClassMethod Test5(val = 3) [ ProcedureBlock = 0 ]{
s:'$d(addr) addr(1)=1, (addr(2),addr(8),addr(9))=289, addr(3)=3
d $case($g(addr(val)),1:t5case1,289:t5case289,3:t5case3,:t5case)
Q
t5case1 w !, "Case 1" Q
t5case3 w !, "Case 3" Q
t5case289 w !, "Case 2 or 8 or 9" Q
t5case w !,"Case default" Q
}
ClassMethod Run() {
s repeat = 1000
s ts = $zh for i=1:1:repeat { d ..Test( $r(10)+1 ) } s t1 = ( $zh - ts )
s ts = $zh for i=1:1:repeat { d ..Test2( $r(10)+1 ) } s t2 = ( $zh - ts )
s ts = $zh for i=1:1:repeat { d ..Test3( $r(10)+1 ) } s t3 = ( $zh - ts )
s ts = $zh for i=1:1:repeat { d ..Test4( $r(10)+1 ) } s t4 = ( $zh - ts )
s ts = $zh for i=1:1:repeat { d ..Test5( $r(10)+1 ) } s t5 = ( $zh - ts )
w !, "do @( ^||addr() ) : ", t1
w !, "do @( addr() ) : ", t2
w !, "if val=case Q : ", t3
w !, "do $case( ^||addr() ) : ", t4
w !, "do $case( addr() ) : ", t5
}
Output:
do @( ^||addr() ) : .019535
do @( addr() ) : .016482
if val=case Q : .015183
do $case( ^||addr() ) : .015971
do $case( addr() ) : .015078
instead of using a magic number 10000, it would be better to use a macro $$$MaxLocalLength, see %Stream.FileBinary:ReadLine