Skip to content

optimized "init" and "create"#264

Merged
mausch merged 1 commit intofsprojects:masterfrom
goswinr:patch-1
Oct 18, 2013
Merged

optimized "init" and "create"#264
mausch merged 1 commit intofsprojects:masterfrom
goswinr:patch-1

Conversation

@goswinr
Copy link
Copy Markdown
Contributor

@goswinr goswinr commented Oct 10, 2013

this versions of "init" without the seq expression runs about 15 times faster for me. my comparison:
is there a reason to not do it like this ?

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

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
@ovatsus
Copy link
Copy Markdown

ovatsus commented Oct 10, 2013

These two functions were copied from the F# PowerPack, probably no one bothered to optimize them, but I don't see any problem on using the loop instead. Probably the main perf difference was because the capacity was not being initialized

mausch added a commit that referenced this pull request Oct 18, 2013
optimized "init" and "create"
@mausch mausch merged commit d9cf4ba into fsprojects:master Oct 18, 2013
@mausch
Copy link
Copy Markdown
Member

mausch commented Oct 18, 2013

Thanks!

@goswinr goswinr deleted the patch-1 branch February 10, 2014 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants