Skip to content

Instantly share code, notes, and snippets.

@Szer
Last active January 31, 2018 20:09
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 Szer/44678b746e5229638e08de84dfdb590b to your computer and use it in GitHub Desktop.
Save Szer/44678b746e5229638e08de84dfdb590b to your computer and use it in GitHub Desktop.
Word count
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace ConsoleApp9
{
class Program
{
static void Main(string[] args)
{
var results = BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).Run(args);
foreach (var res in results)
{
Console.WriteLine(res);
}
Console.ReadLine();
}
}
public class Worker
{
public static string[] Data = File.ReadLines(@"SOME_FILE").ToArray();
[Benchmark(OperationsPerInvoke = 4)]
public (string Word, int Count)[] Dict()
{
var tbl = new Dictionary<string, int>();
foreach (var line in Data)
{
tbl[line] = tbl.ContainsKey(line) ? tbl[line] += 1 : tbl[line] = 1;
}
return tbl.Select(kv => (kv.Key, kv.Value)).ToArray();
}
[Benchmark(OperationsPerInvoke = 4)]
public (string Word, int Count)[] Linq()
{
return Data.GroupBy(x => x).Select(x => (x.Key, x.Count())).ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment