Skip to content

Instantly share code, notes, and snippets.

@manofstick
Last active September 4, 2016 09:35
Show Gist options
  • Save manofstick/08cc49dbd1b8d0d91571e2630e6462b6 to your computer and use it in GitHub Desktop.
Save manofstick/08cc49dbd1b8d0d91571e2630e6462b6 to your computer and use it in GitHub Desktop.
let cli = new System.Net.WebClient ()
let data =
cli.DownloadString "https://raw.githubusercontent.com/dwyl/english-words/master/words.txt"
|> fun s -> s.Split ([|"\n"; "\r"; System.Environment.NewLine|], System.StringSplitOptions.None)
printfn "found %d words - shuffling" data.Length
let r = System.Random ()
for i = 0 to data.Length-1 do
let j = r.Next (i, data.Length-1)
let t = data.[j]
data.[j] <- data.[i]
data.[i] <- t
printfn "shuffled."
let rec buildMap m n =
if n = data.Length then m
else
let item = data.[n]
buildMap (m |> Map.add item item.Length) (n+1)
for i = 1 to 3 do
System.GC.Collect ()
let sw = System.Diagnostics.Stopwatch.StartNew ()
let wordMap = buildMap Map.empty 0
let wordCount = wordMap.Count
printfn "build: %d (%d)" sw.ElapsedMilliseconds wordCount
let rec countLength words n totalLength =
if n = data.Length then totalLength
else
let length =
words
|> Map.find data.[n]
countLength words (n+1) (totalLength+length)
let wordMap = buildMap Map.empty 0
for i = 1 to 3 do
System.GC.Collect ()
let sw = System.Diagnostics.Stopwatch.StartNew ()
let totalLength = countLength wordMap 0 0
printfn "lookup: %d (%d)" sw.ElapsedMilliseconds totalLength
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment