10

I'm new to the language F# and currently I'm doing a short study on the F# performance. So what I would like to do is benchmark my F# code. I've already found the Stopwatch class, which was pretty obvious, but as for the rest of what you should be able to test, I found no clear leads.

What I would wish to do is to find out memory and cpu usage of the F# code when I run it. Is there anyone here that might be able to give me some advices on how to do this?

pad
  • 41,040
  • 7
  • 92
  • 166
Marcus Åberg
  • 213
  • 2
  • 10
  • possible duplicate of [Measure time of execution in F#](http://stackoverflow.com/questions/4528355/measure-time-of-execution-in-f) – vcsjones Apr 06 '12 at 15:30
  • @Marcus - are you looking to measure both memory and CPU? What are the other counters you are looking to check? – Gangadhar Apr 06 '12 at 15:40
  • 2
    @vcsjones He's asking for memory usage--not the same as execution time. I don't think this is a dupe. – Onorio Catenacci Apr 06 '12 at 16:07
  • These days, [BenchmarkDotNet](https://benchmarkdotnet.org/) is generally a good (perhaps even the normally-recommended) choice for benchmarking in both C# _and_ F#. – Jarak Dec 18 '18 at 21:54

3 Answers3

12

For quick prototyping, fsi's timing directive provides good indications:

#if INTERACTIVE
#time "on"
#endif

It prints out something like:

Real: 00:00:00.199, CPU: 00:00:00.120, GC gen0: 0, gen1: 0, gen2: 0 

Beside execution time, other statistics show amounts of garbage collected in three levels of the generational GC, which are helpful to understand memory usage in your programs.

For serious benchmarking, you should use Visual Studio profiler which provides function call counts, timing, and memory allocation measurement, etc or even use third-party memory profilers. You can find some hints of using these profilers in my answer at Good F# Performance Profiling Tool.

Community
  • 1
  • 1
pad
  • 41,040
  • 7
  • 92
  • 166
  • Just keep in mind, that fsi is 32-bit, even if you're on a 64-bit OS. – John Reynolds Apr 06 '12 at 15:46
  • 8
    @JohnReynolds: That's not necessarily true. F# 3.0 has a new 64-bit `fsi`, more information is [here](http://blogs.msdn.com/b/fsharpteam/archive/2012/02/29/introducing-visual-f-3-0-beta.aspx). – pad Apr 06 '12 at 15:51
2

You may also find this blog posting from Dave Thomas (formerly Moirae Software) helpful:

https://7sharp9.github.io/2011/12/11/2011-12-11-fixing-a-hole/

Normally I'd identify the specific portion of the blog posting that I'm thinking answers the question but the entire blog post is as an answer to this question.

EDIT: Updated link since old link was broken.

Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
0

For measuring CPU, this seems to work.

namespace CpuStopwatch

open System
open System.Diagnostics

type State = Stopped | Running

type Options = Total | User
 
type CpuStopwatch(showTotal :Options) =

  let process = Process.GetCurrentProcess()

  let now() =
    match showTotal with
    | Total -> process.TotalProcessorTime
    | User  -> process.UserProcessorTime

  let mutable state         = Stopped
  let mutable latestNow     = now()
  let mutable latestElapsed = TimeSpan.Zero

  let elapsed() =
    match state with
    | Stopped -> latestElapsed
    | Running -> latestElapsed + now() - latestNow

  let start() =
    if state = Stopped then
      latestNow <- now()
      state     <- Running

  let stop() =
    if state = Running then
      latestElapsed <- elapsed()
      state         <- Stopped
  
  member this.Elapsed    with get() = elapsed()
  member this.IsRunning  with get() = state = Running
  member this.IsStopped  with get() = state = Stopped
  
  member this.Start() = start()
  member this.Stop () = stop ()
silvalli
  • 295
  • 2
  • 13