-
-
Save anonymous/0adf47e3c6592f592a2c to your computer and use it in GitHub Desktop.
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
public static class TestingClass | |
{ | |
static void Main() | |
{ | |
StringBuilder sb = new StringBuilder("WhereSelect,Select\n"); | |
Random rnd = new Random(); | |
const int total = 10000; | |
const int onePercent = 100; | |
var startList = Enumerable.Range(1, total).Select(i => new MyClass { Value = rnd.Next(0, 10) }).Batch(onePercent).ToList(); | |
int selectSumTally = 0; | |
int whereSelectSumTally = 0; | |
int tieTally = 0; | |
Console.WriteLine("How much do you want to be the disambiguation percentage?"); | |
decimal percentToTieFor = decimal.Parse(Console.ReadLine() ?? "1.0"); | |
Console.WriteLine("File path for csv?"); | |
string filePath = Console.ReadLine(); | |
Console.WriteLine("Starting benchmarking."); | |
for (int i = 0; i <= 100; i++) | |
{ | |
int i1 = i; | |
var testList = | |
startList | |
.SelectMany((o, inx) => (inx < i1) ? o.Select(mc => ChangeVal(mc, true)) : o.Select(mc => ChangeVal(mc, false))) | |
.ToList(); | |
var selectSum = TimeSpan.FromTicks(0); | |
var whereSelectSum = TimeSpan.FromTicks(0); | |
for (int n = 0; n < 100; n++) | |
selectSum += SelectSumTest(testList); | |
GC_CompleteCollect(); | |
for (int n = 0; n < 100; n++) | |
whereSelectSum += WhereSelectSumTest(testList); | |
if (whereSelectSum.Ticks.IsWithin(percentToTieFor, selectSum.Ticks)) | |
tieTally += 1; | |
else if (whereSelectSum.Ticks < selectSum.Ticks) | |
whereSelectSumTally += 1; | |
else | |
selectSumTally += 1; | |
sb.AppendFormat("{0},{1}\n", whereSelectSum.Ticks, selectSum.Ticks); | |
//uncomment to see which iteration it's at | |
//Console.WriteLine("Iteration {0} done.", i); | |
} | |
File.WriteAllText(filePath ?? "", sb.ToString()); | |
Console.WriteLine("Ties: {0}", tieTally); | |
Console.WriteLine("Where + Select: {0}", whereSelectSumTally); | |
Console.WriteLine("Select: {0}", selectSumTally); | |
Console.ReadKey(true); | |
} | |
private static TimeSpan SelectSumTest(IEnumerable<MyClass> testEnumerable) | |
{ | |
GC_CompleteCollect(); | |
Stopwatch sw = Stopwatch.StartNew(); | |
var result = testEnumerable.Select(mc => mc.IsValid ? mc.Value : 0).Sum(); | |
sw.Stop(); | |
GC_CompleteCollect(); | |
return sw.Elapsed; | |
} | |
private static TimeSpan WhereSelectSumTest(IEnumerable<MyClass> testEnumerable) | |
{ | |
GC_CompleteCollect(); | |
Stopwatch sw = Stopwatch.StartNew(); | |
var result = testEnumerable.Where(mc => mc.IsValid).Select(mc => mc.Value).Sum(); | |
sw.Stop(); | |
GC_CompleteCollect(); | |
return sw.Elapsed; | |
} | |
private static void GC_CompleteCollect() | |
{ | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
} | |
private static MyClass ChangeVal(MyClass mc, bool b) | |
{ | |
mc.IsValid = b; | |
return mc; | |
} | |
} | |
public class MyClass | |
{ | |
public int Value { get; set; } | |
public bool IsValid { get; set; } | |
} | |
public static class Extensions | |
{ | |
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> source, int size) | |
{ | |
Debug.Assert(source != null); | |
Debug.Assert(size > 0); | |
T[] bucket = null; | |
var count = 0; | |
foreach (var item in source) | |
{ | |
if (bucket == null) | |
{ | |
bucket = new T[size]; | |
} | |
bucket[count++] = item; | |
// The bucket is fully buffered before it's yielded | |
if (count != size) | |
{ | |
continue; | |
} | |
// Select is necessary so bucket contents are streamed too | |
yield return bucket.Select(x => x); | |
bucket = null; | |
count = 0; | |
} | |
// Return the last bucket with all remaining elements | |
if (bucket != null && count > 0) | |
{ | |
yield return bucket.Take(count); | |
} | |
} | |
public static bool IsWithin(this long val, decimal percent, long of) | |
{ | |
var sorted = new[] { val, of }.OrderBy(l => l).ToArray(); | |
return sorted[1] - (sorted[1] * (percent / 100)) <= sorted[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment