concept performance in category f#

appears as: Performce, performce
F# Deep Dives

This is an excerpt from Manning's book F# Deep Dives.

  • Performance profile similar to C# —In general, most programs written in F# will have the same performance profile as C# programs. There are some differences: F# has a number of features, such as inlining, that can make F# code faster, and some functional constructs need to be used with care.
  • In video games and front office trading systems, performance is often the key quality issue. Arcade games typically run at 60 frames per second so they feel responsive to the player. Trading screens must also respond to user input in milliseconds, particularly during periods of high market activity, so users can take positions and (often more important) exit those positions if the market turns against them. As discussed in chapter 1, F# is compiled and efficient. We’ll look at techniques for monitoring performance to make sure your F# code satisfies the performance requirements of your project.

    For many line of business (LOB) applications, performance is less of an issue, and the focus is on building out functionality. Getting precise requirements isn’t always easy to achieve, because often customers don’t know at the start exactly what they want. To rapidly respond to changing business requirements, code should be either extensible or modular so that parts can be replaced in isolation. In video games, the game logic is often written in a scripting language like Lua, for rapid prototyping and easy change. In F#, you can achieve similar flexibility using domain-specific languages (DSLs) like the one discussed in chapter 3. Together with the interactive style of development, these are two important reasons F# has a faster time-to-market cycle.

    As discussed earlier, exploratory testing can be also used to check nonfunctional requirements like performance. In FSI, the #time directive turns timing of expressions on or off. You can use it to check the performance of your function:

    > #time;;
    --> Timing now on
    
    > let s = System.String(Array.create 10000000 'Z');;
    Real: 00:00:00.039, CPU: 00:00:00.046, GC gen0: 0, gen1: 0, gen2: 0
    val s : System.String = "ZZZZZZZZZZZ"+[9999989 chars]
    
    > wordPoints s;;
    Real: 00:00:00.097, CPU: 00:00:00.093, GC gen0: 0, gen1: 0, gen2: 0
    val it : int = 100000000

    In the Scrabble application, calculating the score of a word consisting of 1 million letters in less than 100 milliseconds is certainly fast enough. But if performance was your key concern, you could easily try changing the functions to run faster. For example, you could replace the Seq.sumBy function with a mutable variable and iteration:

    > let wordPointsMutable (word:string) =
        let mutable sum = 0
        for i = 0 to word.Length - 1 do
          sum <- sum + letterPoints(word.[i])
        sum;;
    (...)
    
    > wordPointsMutable s;;
    Real: 00:00:00.032, CPU: 00:00:00.031, GC gen0: 0, gen1: 0, gen2: 0
    val it : int = 100000000

    This version of the function is longer and harder to write, but it’s about three times faster. Which of the functions should you use? This is why understanding nonfunctional requirements is important. For most applications, simplicity and readability are preferred, so the original implementation is better. But if performance is your key concern, you can choose based on a simple explorative test. Now, let’s look at the next step of the testing process: unit testing.