Would not seem that Which[] would be a problem in compiling functions, but it's doing a call to Main Evaluate - its obviously on the list of compiled functions and pretty basic. Running the simple code below illustrates it (in v13.1). Its does not happen with If[] as one would expect. Really like to use Which[], but can a call to Main Evaluate be avoided?
Thanks in advance.
<< CompiledFunctionTools`;
fun = Compile[{{k, _Integer}, {u, _Real}},
Which[k == 1, u + 1, k == 2, u + 4]];
funIf = Compile[{{k, _Integer}, {u, _Real}}, If[k == 1, u + 1, u + 4]];
fun[1, 2]
funIf[1, 2]
CompilePrint[funIf]
CompilePrint[fun]
For Which [] the result without the header is
I0 = A1
R0 = A2
Result = R1
R1=MainEvaluate[Function[{k,u},Which[k==1,u+1,k==2,u+4]][I0,R0]]
Return
and for IF the result without the header is
I0 = A1
R0 = A2
I2 = 4
I1 = 1
Result = R1
B0=I0==I1
2 if[!B0] goto 7
3 R1=I1
4 R2=R0+R1
5 R1=R2
6 goto 10
7 R1=I2
8 R3=R0+R1
9 R1=R3
10 Return