Last active
September 20, 2019 04:09
-
-
Save pete-murphy/9a2b698e05f7a865ec208bd4ef932820 to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE TupleSections #-} | |
module Main where | |
import Criterion.Main | |
import Data.Char (toLower) | |
import Data.Function (on) | |
import Data.List (sort) | |
import Data.Map (fromListWith) | |
isAnagramOf :: String -> String -> Bool | |
isAnagramOf = (==) `on` (sort . fmap toLower) | |
isAnagramOf' :: String -> String -> Bool | |
isAnagramOf' = (==) `on` (freqs . fmap toLower) | |
where | |
freqs = fromListWith (+) . fmap (, 1) | |
main :: IO () | |
main = do | |
everyWord <- lines <$> readFile "/usr/share/dict/words" | |
defaultMain | |
[ bench "anagrams, sort" $ nf (fmap $ isAnagramOf "banana") everyWord | |
, bench "anagrams, freqs" $ nf (fmap $ isAnagramOf' "banana") everyWord | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would think that
isAnagramOf'
would be more efficient thanisAnagramOf
(sincefreqs
is O(n), andsort
must be less efficient than that) but I consistently get similar benchmarks