5

Is there anything I can do to make f# code compile faster apart from (or better yet instead of) running ngen?

And what is approximate benchmark "hello world" compilation time (or better said compiler startup time+compilation time) for say pentium 4 machine?

Edit There are nuances as it turns out:)

So first, can anyone explain what is start-up time of compiler? And why is it slow. Also links to information on the whole f# compilation process would be appreciated.

Context: f# compiler is repeatedly invoked on small snippets of code through

using (CodeDomProvider provider = new Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider())
{
  //start time
  cr = provider.CompileAssemblyFromSource(cp, new string[] { data.Program });
  //end time
}

The time difference is ~6 sec. So the question basically is what can be done, apart from ngen? You can see for yourself here:rundotnet

Cœur
  • 37,241
  • 25
  • 195
  • 267
ren
  • 3,843
  • 9
  • 50
  • 95
  • Benchmarking compilation of "hello world" is *wrong*, you'll only measure startup overhead, not real compiler performance. – Mauricio Scheffer Aug 20 '11 at 19:09
  • possible duplicate of [f# compiling too slow](http://stackoverflow.com/questions/4697771/f-compiling-too-slow) – Mauricio Scheffer Aug 20 '11 at 19:13
  • maybe this should be different topic, but what is startup, and if it is possible to "startup" once if I have to compile many times? – ren Aug 20 '11 at 19:15
  • In particular, Chris Smith explains there why the F# compiler will probably never be as fast as the C# compiler – Mauricio Scheffer Aug 20 '11 at 19:15
  • What do you find slow? Startup or actual compilation? – Mauricio Scheffer Aug 20 '11 at 19:16
  • Which compiler do you mean? The F# compiler or the JIT compiler? – svick Aug 20 '11 at 19:20
  • @Mauricio Scheffer Not sure, I just invoke f# compiler through Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider and the whole thing is slow. I guess I should correct the question. – ren Aug 20 '11 at 19:23
  • How slow is it (in lines/s)? Why are you using FSharpCodeProvider instead of fsc.exe? Can you post the code? – Mauricio Scheffer Aug 20 '11 at 19:29
  • the code compiled dynamically from c#, that's why FSharpCodeProvider, I guess fsc.exe could be called from c# too, if this would be faster – ren Aug 20 '11 at 19:42
  • Startup time: the time that goes from you hitting 'enter' on the command-line calling fsc.exe until the compiler actually starts reading the code to compile (this includes OS overheads, .NET startup, any fsc initializations, etc) – Mauricio Scheffer Aug 20 '11 at 19:45
  • And I think if you measure the compilation of something trivial as "hello world", it would be something like 95% startup overhead and 5% actual compilation... – Mauricio Scheffer Aug 20 '11 at 19:59
  • On my machine I find it very helpful to switch from "Balanced" to "High Performance" power plan. For the project I am working on, startup times are reduced by a factor of 2, and fsc.exe seems to be favorably affected as well. Not sure how it works: either the OS starts using more cores or does not throttle CPU. – t0yv0 Aug 22 '11 at 14:20

1 Answers1

5

This takes 0.9s on my computer (AMD Athlon 64 X2 4600):

#r "FSharp.Compiler.CodeDom"

open System.Reflection
open System.CodeDom.Compiler
open Microsoft.FSharp.Compiler.CodeDom

let comp() =
    use provider = new FSharpCodeProvider() 
    let code = "module pp\n printfn \"Hello world\""
    let cp = new CompilerParameters() 
    cp.GenerateInMemory <- true 
    provider.CompileAssemblyFromSource(cp, [|code|]) |> ignore
#time
comp()
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275