Skip to content

Instantly share code, notes, and snippets.

@bisserlis
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bisserlis/9c92a63fcb9feeaa768c to your computer and use it in GitHub Desktop.
Save bisserlis/9c92a63fcb9feeaa768c to your computer and use it in GitHub Desktop.
filterByList Core comparison
[1 of 1] Compiling Elegant ( elegant.hs, elegant.o )
==================== Tidy Core ====================
Result size of Tidy Core = {terms: 20, types: 32, coercions: 0}
Elegant.filterByList1
:: forall a_b. GHC.Types.Bool -> a_b -> [a_b] -> [a_b]
[GblId,
Arity=3,
Caf=NoCafRefs,
Unf=Unf{Src=<vanilla>, TopLvl=True, Arity=3, Value=True,
ConLike=True, WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(unsat_ok=True,boring_ok=True)}]
Elegant.filterByList1 =
\ (@ a_b)
(x_agM :: GHC.Types.Bool)
(y_agN :: a_b)
(r_agO :: [a_b]) ->
case x_agM of _ {
GHC.Types.False -> r_agO;
GHC.Types.True -> GHC.Types.: @ a_b y_agN r_agO
}
Elegant.filterByList
:: forall a_aeG. [GHC.Types.Bool] -> [a_aeG] -> [a_aeG]
[GblId,
Arity=2,
Caf=NoCafRefs,
Str=DmdType SL,
Unf=Unf{Src=<vanilla>, TopLvl=True, Arity=2, Value=True,
ConLike=True, WorkFree=True, Expandable=True,
Guidance=IF_ARGS [0 0] 50 0}]
Elegant.filterByList =
\ (@ a_b) (ps_aeI :: [GHC.Types.Bool]) (as_aeJ :: [a_b]) ->
GHC.List.foldr2
@ GHC.Types.Bool
@ a_b
@ [a_b]
(Elegant.filterByList1 @ a_b)
(GHC.Types.[] @ a_b)
ps_aeI
as_aeJ
module Main where
filterByList :: [Bool] -> [a] -> [a]
filterByList ps as = map snd . filter fst $ zip ps as
main :: IO ()
main = print . length $ filterByList (cycle [True, False]) [1..100000]
[1 of 1] Compiling Longhand ( longhand.hs, longhand.o )
==================== Tidy Core ====================
Result size of Tidy Core = {terms: 24, types: 28, coercions: 0}
Rec {
Longhand.filterByList [Occ=LoopBreaker]
:: forall a_aeG. [GHC.Types.Bool] -> [a_aeG] -> [a_aeG]
[GblId, Arity=2, Caf=NoCafRefs, Str=DmdType SL]
Longhand.filterByList =
\ (@ a_b) (ds_deQ :: [GHC.Types.Bool]) (ds1_deR :: [a_b]) ->
case ds_deQ of _ {
[] -> GHC.Types.[] @ a_b;
: ipv_sfa ipv1_sfb ->
case ds1_deR of _ {
[] -> GHC.Types.[] @ a_b;
: ipv2_sfe ipv3_sff ->
case ipv_sfa of _ {
GHC.Types.False -> Longhand.filterByList @ a_b ipv1_sfb ipv3_sff;
GHC.Types.True ->
GHC.Types.:
@ a_b ipv2_sfe (Longhand.filterByList @ a_b ipv1_sfb ipv3_sff)
}
}
}
end Rec }
module Main where
filterByList :: [Bool] -> [a] -> [a]
filterByList [] _ = []
filterByList _ [] = []
filterByList (True :ps) (a:as) = a : filterByList ps as
filterByList (False:ps) (_:as) = filterByList ps as
main :: IO ()
main = print . length $ filterByList (cycle [True, False]) [1..100000]
bissbook:mapfiltbench bisserlis$ ghc -O2 -ddump-simpl longhand.hs > longhand.core.txt
bissbook:mapfiltbench bisserlis$ ghc -O2 -ddump-simpl elegant.hs > elegant.core.txt
bissbook:mapfiltbench bisserlis$ time ./longhand
50000
real 0m0.069s
user 0m0.004s
sys 0m0.002s
bissbook:mapfiltbench bisserlis$ time ./elegant
50000
real 0m0.012s
user 0m0.009s
sys 0m0.002s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment