-
Notifications
You must be signed in to change notification settings - Fork 50
Improve foldr performance ~4x on large lists #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,32 @@ | ||
module Bench.Data.List where | ||
|
||
import Prelude | ||
import Data.Foldable (maximum) | ||
import Data.Int (pow) | ||
import Data.List (List(..), take, range, foldr, length) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Traversable (traverse_) | ||
import Effect (Effect) | ||
import Effect.Console (log) | ||
import Performance.Minibench (bench) | ||
|
||
import Data.List as L | ||
|
||
benchList :: Effect Unit | ||
benchList = do | ||
log "map" | ||
log "---" | ||
benchMap | ||
benchLists "map" $ map (_ + 1) | ||
benchLists "foldr" $ foldr add 0 | ||
|
||
where | ||
|
||
benchMap = do | ||
let nats = L.range 0 999999 | ||
mapFn = map (_ + 1) | ||
list1000 = L.take 1000 nats | ||
list2000 = L.take 2000 nats | ||
list5000 = L.take 5000 nats | ||
list10000 = L.take 10000 nats | ||
list100000 = L.take 100000 nats | ||
|
||
log "map: empty list" | ||
let emptyList = L.Nil | ||
bench \_ -> mapFn emptyList | ||
|
||
log "map: singleton list" | ||
let singletonList = L.Cons 0 L.Nil | ||
bench \_ -> mapFn singletonList | ||
|
||
log $ "map: list (" <> show (L.length list1000) <> " elems)" | ||
bench \_ -> mapFn list1000 | ||
|
||
log $ "map: list (" <> show (L.length list2000) <> " elems)" | ||
bench \_ -> mapFn list2000 | ||
|
||
log $ "map: list (" <> show (L.length list5000) <> " elems)" | ||
bench \_ -> mapFn list5000 | ||
listSizes = Cons 0 $ map (pow 10) $ range 0 5 | ||
nats = range 0 $ (fromMaybe 0 $ maximum listSizes) - 1 | ||
lists = map (\n -> take n nats) listSizes | ||
|
||
log $ "map: list (" <> show (L.length list10000) <> " elems)" | ||
bench \_ -> mapFn list10000 | ||
benchLists :: forall b. String -> (List Int -> b) -> Effect Unit | ||
benchLists label func = | ||
traverse_ (benchAList label func) lists | ||
|
||
log $ "map: list (" <> show (L.length list100000) <> " elems)" | ||
bench \_ -> mapFn list100000 | ||
benchAList :: forall a b. String -> (List a -> b) -> List a -> Effect Unit | ||
benchAList label func list = do | ||
log "---" | ||
log $ label <> ": list (" <> show (length list) <> " elems)" | ||
bench \_ -> func list |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the core change. Wondering if the
reverse
function should be moved fromData.List
to this file to deduplicate code.