Last active
January 31, 2018 20:09
-
-
Save Szer/44678b746e5229638e08de84dfdb590b to your computer and use it in GitHub Desktop.
Word count
This file contains 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
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