.F# Performance Guidelines
This page discusses hints and tips regarding F# performance in general, with some specifics on performance when porting OCaml code to F#. Each entry references the version of F# where the issue was last verfied as existing.
OCaml to F# Porting Performance
- Prefer structs to tuples for frequent assignments (May 2009 CTP). In OCaml, tuples are generally compiled as structs which live on the stack, and therefore are quite fast and may be compiled away. In F#, however, assignment and storage using tuples as well as tuple "let" binding (let (a,b) = (1,2)), causes creation of a heap-allocated object. This is to provide compatiblity with the .NET 4.0 Tuple type. In F#, assignments using structs, on the other hand, are either optimized away or will place data on the stack, for a significant performance improvement.
- Do not use exceptions for flow control (May 2009 CTP/.NET Runtime). In OCaml, exception handling is quite fast (they may even be optimized away by the compiler). In F#, though, exception handling always relies on .NET exception processing, which is orders of magnitude slower. Therefore, rewrite loops which use exception handling for flow control into either loops with one or more mutable control variables, or as tail-recursive functions (see http://cs.hubfs.net/forums/post/10157.aspx ), or using workflows/monads (see http://tomasp.net/articles/imperative-ii-break.aspx ). Performance of the workflow solution has not been evaluated, though.
- Note that the tailcalls are not always supported properly on .NET x64 versions, so may cause a stack overflow (.NET 3.5 Runtime).
Education school | Online early education degree | Teaching Assistant degree | Engineering degree | Online Civil engineering degree
OCaml is the ML dialect and implementation that acted as one of the inspirations for F#
7/9/2006 12:49:17 PM - -81.146.14.140
OCaml is the ML dialect and implementation that acted as one of the inspirations for F#
7/9/2006 12:49:17 PM - -81.146.14.140
OCaml is the ML dialect and implementation that acted as one of the inspirations for F#
7/9/2006 12:49:17 PM - -81.146.14.140
OCaml is the ML dialect and implementation that acted as one of the inspirations for F#
7/9/2006 12:49:17 PM - -81.146.14.140