2 people like it.

iter & map speed comparison (array vs list vs ResizeArray)

A simple test to compare how much time it takes to iterate through an array/list/ResizeArray.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
let n = 10000000
let arr = Array.init n (fun _ -> 0)

let rec buildList n acc i = if i = n then acc else buildList n (0 :: acc) (i + 1)
let lst = buildList n [] 0

let rezArr = new ResizeArray<int>()
rezArr.AddRange(arr)

printfn "Array has [%d] elements" arr.Length
printfn "List has [%d] elements" lst.Length
printfn "ResizeArray has [%d]" rezArr.Count

#time

let doNothing _ = ()

arr |> Array.iter doNothing
lst |> List.iter doNothing
rezArr |> Seq.iter doNothing

let incr x = x + 1

arr |> Array.map incr
lst |> List.map incr
new ResizeArray<int>(rezArr |> Seq.map incr)
val n : int

Full name: Script.n
val arr : int []

Full name: Script.arr
module Array

from Microsoft.FSharp.Collections
val init : count:int -> initializer:(int -> 'T) -> 'T []

Full name: Microsoft.FSharp.Collections.Array.init
val buildList : n:int -> acc:int list -> i:int -> int list

Full name: Script.buildList
val n : int
val acc : int list
val i : int
val lst : int list

Full name: Script.lst
val rezArr : ResizeArray<int>

Full name: Script.rezArr
type ResizeArray<'T> = System.Collections.Generic.List<'T>

Full name: Microsoft.FSharp.Collections.ResizeArray<_>
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
System.Collections.Generic.List.AddRange(collection: System.Collections.Generic.IEnumerable<int>) : unit
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property System.Array.Length: int
property List.Length: int
property System.Collections.Generic.List.Count: int
val doNothing : 'a -> unit

Full name: Script.doNothing
val iter : action:('T -> unit) -> array:'T [] -> unit

Full name: Microsoft.FSharp.Collections.Array.iter
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val iter : action:('T -> unit) -> list:'T list -> unit

Full name: Microsoft.FSharp.Collections.List.iter
module Seq

from Microsoft.FSharp.Collections
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val incr : x:int -> int

Full name: Script.incr
val x : int
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
Raw view Test code New version

More information

Link:http://fssnip.net/cF
Posted:13 years ago
Author:theburningmonk
Tags: