f# compiling too slow

2019-01-13 22:13发布

问题:

I'm new to f#. I downloaded the Visual Studio 2010 shell and the F# ctp and wrote a small hello world script with the following code

printfn "Hello World"
let _ = System.Console.ReadLine()

This takes around 13 to 15 seconds to compile which is very slow compared to running a similar C# script(which takes around 2 secs). I'd like the F# script to compile faster so that my development(i.e. experimentation) time would be reduced, I don't care for the runtime performance.

Is there any way to make the F# script compile faster, maybe turn on/off some Build settings in Visual Studio or something like that?

FYI, I'm using a 4 year old pentium 4, 1.5 gb RAM machine, if that helps.

回答1:

I have no idea how fast a Pentium 4 should compile that "hello world" program, but 15 seconds strikes me as pretty slow. I once had similar speed problems with a VS 2010 Beta and the problem turned out to be that Visual Studio and the F# compiler weren't yet properly NGENed.

Normally, the Visual Studio install should make sure that everything gets NGENed, but maybe something went wrong. You can check if the F# compiler was NGENed with the following command in a console window with admin rights:

cd "C:\Program Files\FSharp-2.0.0.0\bin"
c:\windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe display fsc.exe

If the result of that shows that the native image of fsc.exe is still pending, you could force the compilation with:

c:\windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe executeQueuedItems

Note: I'm not sure which version of the F# compiler you're using exactly. The one used by the full install of VS2010 is the one in C:\Program Files\Microsoft F#\v4.0 (or C:\Program Files (x86)\Microsoft F#\v4.0 on 64-bit machines). So, if you use that one, you have to cd into that folder instead of the C:\Program Files\FSharp-2.0.0.0\bin folder.



回答2:

Unfortunately there isn't much you can do -- the F# compiler is just slower than the C# compiler. But let me explain why:

Why the F# compiler is slower than the C# compiler

First, the F# compiler is written on .NET, the C# compiler is written in C++. While this alone isn't a death sentence to perf, it does make a difference. Second, the C# compiler is 10+ years old. There has been a lot of time to tune and optimize it -- both the compiler itself and the .NET runtime. The .NET JIT engine has been fine-tuned for C#/VB.NET and not F#. Functional programming requires a lot of short-lived objects, which translates to different type of GC behavior.

But the real reason why the F# compiler is noticeably slower than the C# compiler is because it is doing more work than the C# compiler. In C# you provide all of the type information, which in a way is you doing work for the compiler. F# on the other hand does type inference for you, which while saving you from the burden of annotation does require additional CPU time.

What you can do

I recommend you download the Visual Studio 2008 shell and use F# targeting the .NET Framework 2.0. Unless you need something that is in Visual Studio 2010 or CLR 4.0 only, you will be fine on Visual Studio 2008. The F# language functions the exact same. The only difference, IIRC, is in what types certain things compile to. For example, there is a Tuple<_> type built into CLR 4.0, but when targeting CLR 2.0 the tuple type defined in FSharp.Core.dll is used.

Visual Studio 2010 offers a lot of slick bells and whistles, such as a WPF-based code editor. However, those niceties consume a lot of RAM and in your case it sounds like you can live without them.



回答3:

Note also that you can use F# Interactive to evaluate snippets of code or scripts, and since the FSI window in VS stays open, it is much faster (the startup time for fsc.exe is bad).