In a typical Java program I'd just fire up a profiler (jvisualvm, for instance) and have it tell me which were the hot methods in some kind of CPU Sampling view. I guess this would work exactly the same way were I developing with .fs files in a regular project. As I'm in F# Interactive I'm a bit lost.
Using fsx
files and F# Interactive doesn't mean that you should abandon compiling projects completely. I would change Build Action
in File Properties
to Compile
in order to compile fsx files directly. We need to use conditional compilation at some places e.g.
#if INTERACTIVE
#time "on";;
#endif
The advantages of compiling the code are:
- You can use Visual Studio profilers to get statistics about hot spots in programs.
- You can decompile programs using ILSpy. Sometimes ILs or even equivalent C# code can give you insights on how the programs behave.
As the code evolves, you can consider moving core functions to fs
files and keep quick profiling functions in fsx
files.
Back to your example, an improvement of toDigits'
is to avoid using references:
let toDigits'' (x:bigint) =
let rec loop x acc =
if x <> 0I then
loop (x/10I) (int(x%10I)::acc)
else acc
loop x [] |> List.toArray
Results show that toDigits''
is 1.5x faster than toDigits'
and 1.5x slower than toDigits
.
It's hard to beat toDigit
since it doesn't use any arithmetic operations on bigint
. One obvious drawback of toDigit
is that it gives meaningless results on negative bigint
s.