I wanted to look at the pros & cons of Compile & FunctionCompile. This code below is from Neat Examples in FunctionCompile documentation.
restrictFlow[ x_, y_]:=Module[{z},
z=1-1/(x+I y)^2;
{{x,y},{Re[z],-Im[z]}}
];
decl=FunctionDeclaration[restrictFlow, Typed[{"Real64","Real64"}->"PackedArray"::["Real64",2]]@DownValuesFunction[restrictFlow]];
func=Function[{Typed[x0,"Real64"],Typed[x1,"Real64"],Typed[y0,"Real64"], Typed[y1,"Real64"]},
Table[Flatten[ Table[{restrictFlow[x+d/4,y], restrictFlow[x+d/4,-y]},{x,x0,x1},{y,y0,y1}],2] ,{d, 0,3, 0.05}]
];
usingFunctionCompile=FunctionCompile[decl, func];
Next I try to do the same thing using Compile.
usingCompiled=Compile[{{x0,_Real},{x1,_Real},{y0,_Real},{y1,_Real}},
Module[{zP,zM},
Table[Flatten[ Table[
zP=1-1/(x+d/4+I y)^2;
zM=1-1/(x+d/4-I y)^2;
{{{x,y},{Re[zP],-Im[zP]}}, {{x,-y},{Re[zM],-Im[zM]}}},
{x,x0,x1},{y,y0,y1}],2] ,{d, 0,3, 0.05}
]
]];
I find that the above implementations return nearly the same results. Any ideas how we can make them return exactly the same results? Now look at the timing comparison.
RepeatedTiming[compileResult = usingCompiled[-7,5,0.5, 3];]
(* {0.00125593, Null} *)
and
RepeatedTiming[functionCompileResult = usingFunctionCompile[-7,5,0.5, 3];]
(* {0.00273739, Null} *)
The CompiledCodeFunction that takes over twice as long to evaluate than the version written with Compile. Coding with Compile is much easier than coding with FunctionCompile. Compile[] evaluates almost instantly, but FunctionCompile[] takes a while to compile. Am I right that we should only use FunctionCompile when we can't use Compile?