Skip to content

Instantly share code, notes, and snippets.

@fearofcode
Last active September 2, 2018 23:06
Show Gist options
  • Save fearofcode/e2e668688edd9d35896f9b9b635836bf to your computer and use it in GitHub Desktop.
Save fearofcode/e2e668688edd9d35896f9b9b635836bf to your computer and use it in GitHub Desktop.
simple C# concurrent word counter based on https://www.youtube.com/watch?v=B5xYBrxVSiE
using System;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Collections.Concurrent;
namespace wordcounter
{
class Program
{
static void Main(string[] args)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
// TODO: take command line parameter
var textDirectory = @"C:\Users\Warren\Downloads\show-master\episode\9\text\";
var finalTally = new ConcurrentDictionary<string, int>();
Directory.GetFiles(textDirectory).AsParallel().ForAll((filePath) =>
{
var fileContents = File.ReadAllText(filePath);
foreach (var word in fileContents.Split(null))
{
var lowercased = word.ToLower();
finalTally.AddOrUpdate(lowercased, 1, (key, oldValue) => oldValue + 1);
}
});
foreach (var entry in finalTally)
{
if (entry.Value > 1)
{
Console.WriteLine(entry);
}
}
stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment