optimized "init" and "create"#264
Merged
mausch merged 1 commit intofsprojects:masterfrom Oct 18, 2013
goswinr:patch-1
Merged
Conversation
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
|
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 |
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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