In this blog, I often talk about efficiency aside data mining as efficiency really matters in this field. Previously, we’ve talked about how to use PInvoke and C++/CLI to use C++ code to boost the performance. In this post, I’d like to start a series on F# itself. As a functional programming language, F# programs are kind of high-level, thus harder to reason its performance than imperative programs. Not the big-O thing, it is the constant. This series contains little tips about performance.
Note that these tips are just facts to let you know. There is no performance guidance that one should follow. There is always a treadoff, high level functional constructs usually are slower than their pure imperative equivalents. However, they are safer and have better abstraction. It usually depends when you really need to do some optimization. E.g. if you could guarantee that your program runs less than 0.5 second, then optimizing it would usually become less meaningful, unless you have another program calling this program a million times.
Also remember that
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" (by Donald Knuth)
1. Stream IO and Seq module
Think about that we need to process a large text file, say, 1TB. Then it is infeasible to load the file into memory. The approach is to treat the text file as a stream and scan this stream to get useful information.
- open System.IO
- let readLinesSeq = File.ReadLines
.Net 4.0 provides ReadLines, the type of its return value is seq<string>.
In lower versions of .Net, one needs to implement this function:
- let readLines filePath = seq {
- use sr = new StreamReader (filePath)
- while not sr.EndOfStream do
- yield sr.ReadLine ()
- }
Once we have this function, we can process it as as a sequence.
While prototyping a model, we may make mistakes, and we also know that scanning the whole file would cost a lot of time. If we find an error after scanning the whole scanning, that would waste a lot of time. So we would like to take the first few lines of the file as a small test set for the debugging purpose. By treating a file as a sequence, we could simply use
- let lines100 = "filename" |> readLines |> Seq.take 100
to get a small test set.
Here is an quite old post in 2006. At that time, there seemed to be no Seq module. The idea is the same.
2. Loop, Seq module and the tail-recursive version of “while”
One of my first F# programs is a prime number test function. I first used an ugly for loop with mutable variables:
- let isPrime1 n =
- let m = int(sqrt (float n))
- let mutable flag = true
- for i=2 to m do
- if n % i = 0 then
- flag <- false
- flag
You can see that I clearly didn’t know how to early stop/break the loop when flag is assigned to false. Because I could not find break, so I crazily use exception to break out the loop:-)
- exception Break
- let isPrime1' n =
- let m = int(sqrt (float n))
- let mutable flag = true
- try
- for i=2 to m do
- if n % i = 0 then
- flag <- false
- raise Break
- with
- _ -> ()
- flag
And based on my FP experience on Scheme, I immediately wrote a tail-recursive function, which behaves like a “while” loop:
- let isPrime2 n =
- let m = int(sqrt (float n))
- let rec loop div =
- if div > m then true
- elif n % div = 0 then false
- else loop (div+1)
- loop 2
I can also explicitly use a while loop with a mutable flag:
- let isPrime3 n =
- let m = int(sqrt (float n))
- let mutable flag = true
- let mutable div = 2
- while flag && div <= m do
- if n % div = 0 then
- flag <- false
- div <- div + 1
- flag
And I finally find the elegant way using Seq:
- let isPrime n = {2..int(sqrt (float(n)))} |> Seq.forall (fun i->n%i<>0)
However, the last version would be slower than others except for the first one. Because a sequence is actually an object implements IEnumerable interface. If the object currently points to 10, to get 11, it needs to call a Next function to get it. There is definitely some overhead here.
So pay attention when you use Seq to do the job of a for loop or while loop when the loop is executed for a huge number of times.
3. List and Array
F# lists are single linked lists. Thus, Lists use more memory as it needs to keep a next pointer in every node. Lists are also slow. Let’s do a test:
- let n = 10000000
- let a = List.init n (fun i-> i)
- let b = Array.init n (fun i-> i)
creating list a uses about 2.2 seconds and about 200MB memory, while creating array b only uses 0.12 second and about 40MB memory, which equals our estimate n * 4 bytes/int ~= 40MB.
4. Access lists, arrays and sequences
We know that the three modules List, Array and Seq have similar functions. E.g. all of them have the function map, which applies a function to the existing list, array or sequence to generate a new one. However, the three maps are implemented differently, with array most efficiently, list second, the most general seq the last:
- // form F# source code: local.fs
- // note: the actuall implementation for list is in local.fs, not in list.fs
- let map f x =
- match x with
- | [] -> []
- | [h] -> [f h]
- | (h::t) ->
- let cons = freshConsNoTail (f h)
- mapToFreshConsTail cons f t
- cons
- // form F# source code: array.fs
- let map (f: 'T -> 'U) (array : 'T[]) : 'U[]=
- checkNonNull "array" array
- let inputLength = array.Length
- let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
- Parallel.For(0, inputLength, fun i ->
- result.[i] <- f array.[i]) |> ignore
- result
- // form F# source code: seq.fs
- let map f (e : IEnumerator<_>) : IEnumerator<_>=
- upcast
- { new MapEnumerator<_>() with
- member this.DoMoveNext (curr : byref<_>) =
- if e.MoveNext() then
- curr <- (f e.Current)
- true
- else
- false
- member this.Dispose() = e.Dispose()
- }
We can see that the Array.map is actually implemented using ParallelFor. The most general implementation is Seq.map based one the IEnumerable interface in .Net, which is relatively inefficient compared to a for loop.
However, by using Array.map or List.map, we create another array or list. While Seq.map only creates an auxiliary sequence, which costs nearly no memory!
So usually, I use Seq.map to map arrays or lists to keep memory usage small. When performance really matters, I use array and for loop, i.e. I write imperative code. In my experience, writing imperative code in F# sometimes still introduces some runtime errors, while this is rare in pure functional code.
When I only need to iterate an array, I will directly use Array.iter since it is efficient, and use no extra memory.
All in all: performance costs. Do some quick estimation before you write imperative code. Also know the low-level implementation of your daily-use libraries is critical for you to estimate the performance.
5. Access F# matrices and vectors
The Vector<’T> type is implemented using 1-D array and the Matrix<’T> type is implemented using 2-D array for dense case and row-sparse for sparse case. Details are in a matrix series post.
Let m be a matrix, we could also use m.[i,j] to access the element, which is actually a function call to the the internal array. Thus there is some overhead. Some profiling is reported in a previous post.
The way to get rid of this kind of overhead is to program on the internal arrays directly. As in this answer in Stackoverflow.