Skip to content

Commit 4338d34

Browse files
committed
optimized ini & create
this versions of "init" without the seq expression run 20x faster for me. my comparison: #time let createSeq n x = new ResizeArray<_> (seq { for _ in 1 .. n -> x }) let createLoop (n: int) x = let arr = ResizeArray<_>(n) for i=0 to n-1 do arr.Add x arr for i=0 to 1000 do // Real: 00:00:00.506, CPU: 00:00:00.530, GC Gen0: 14, Gen1: 3, Gen2: 3 let r = createSeq 1000 4.4 r.Count |> ignore for i=0 to 1000 do // Real: 00:00:00.016, CPU: 00:00:00.031, GC Gen0: 1, Gen1: 1, Gen2: 1 let r = createLoop 1000 4.4 r.Count |> ignore
1 parent 1ed50ef commit 4338d34

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/FSharpx.Core/Collections/ResizeArray.fs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ module ResizeArray =
1414
let length (arr: ResizeArray<'T>) = arr.Count
1515
let get (arr: ResizeArray<'T>) (n: int) = arr.[n]
1616
let set (arr: ResizeArray<'T>) (n: int) (x:'T) = arr.[n] <- x
17-
let create (n: int) x = new ResizeArray<_> (seq { for _ in 1 .. n -> x })
18-
let init (n: int) (f: int -> 'T) = new ResizeArray<_> (seq { for i in 0 .. n-1 -> f i })
17+
let create (n: int) x =
18+
let arr = ResizeArray<_>(n)
19+
for i=0 to n-1 do arr.Add x
20+
arr
21+
22+
let init (n: int) (f: int -> 'T) =
23+
let arr = ResizeArray<_>(n)
24+
for i=0 to n-1 do arr.Add (f i)
25+
arr
1926

2027
let blit (arr1: ResizeArray<'T>) start1 (arr2: ResizeArray<'T>) start2 len =
2128
if start1 < 0 then invalidArg "start1" "index must be positive"

0 commit comments

Comments
 (0)