Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GreedyLinq
- {
- public class Man
- {
- public int Age { get; set; } = 30;
- public Man ChangeAge(int age)
- {
- Age = age;
- return this;
- }
- }
- public class Test
- {
- Dictionary<Type, List<long[]>> results = new Dictionary<Type, List<long[]>>();
- public void PrintResults()
- {
- foreach (var type in results.Keys)
- {
- var bench = results[type];
- Console.WriteLine("Results for " + type.Name + ":" + Environment.NewLine + Environment.NewLine);
- Console.WriteLine("Jit compiled time: ");
- Console.WriteLine("Linq: " + results[type].First()[0] + " ms");
- Console.WriteLine("Linq: " + results[type].First()[1] + " ticks");
- Console.WriteLine("Calculate: " + results[type].First()[2] + " ms");
- Console.WriteLine("Calculate: " + results[type].First()[3] + " ticks" + Environment.NewLine);
- var avg = new long[4] { 0, 0, 0, 0 };
- for (int j = 1; j < results[type].Count; j++)
- {
- avg[0] += results[type][j][0];
- avg[1] += results[type][j][1];
- avg[2] += results[type][j][2];
- avg[3] += results[type][j][3];
- }
- Console.WriteLine("Avg after Jit time: ");
- Console.WriteLine("Linq: " + avg[0]/(avg.Length - 1) + " ms");
- Console.WriteLine("Linq: " + avg[1] / (avg.Length - 1) + " ticks");
- Console.WriteLine("Calculate: " + avg[2] / (avg.Length - 1) + " ms");
- Console.WriteLine("Calculate: " + avg[3] / (avg.Length - 1) + " ticks" + Environment.NewLine);
- Console.WriteLine(); Console.WriteLine();
- }
- }
- public T[] LinqStart<T>(IEnumerable<T> source)
- {
- var sw = new Stopwatch(); sw.Start();
- IEnumerable<T> middleRezs = null;
- if (source is IEnumerable<Man> src) middleRezs = ManTest(src) as IEnumerable<T>;
- else if (source is IEnumerable<int> isrc) middleRezs = IntTest(isrc) as IEnumerable<T>;
- else return null;
- var lms = sw.ElapsedMilliseconds; var lticks = sw.ElapsedTicks;// here
- var arr = middleRezs.ToArray(); sw.Stop();
- var ams = sw.ElapsedMilliseconds - lms;
- var aticks = sw.ElapsedTicks - lticks;
- var rez = new long[] { lms, lticks, ams, aticks };
- if (results.ContainsKey(typeof(T))) results[typeof(T)].Add(rez);
- else
- results.Add(typeof(T), new List<long[]> { rez });
- return arr;
- }
- private static IEnumerable<Man> ManTest(IEnumerable<Man> source)
- {
- return source
- .Select(m => new Man { Age = m.Age + 1 })
- .Select(m => new Man { Age = m.Age * 2 })
- .Where(c => c.Age % 2 == 0);
- }
- private static IEnumerable<int> IntTest(IEnumerable<int> source)
- {
- return source.Select(c => c + 1).Select(c => c * 2).Where(c => c % 2 == 0);
- // here
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("press either key for the test");
- Console.Read();
- var test = new Test();
- List<Man> manList = new List<Man>();
- List<int> intList = new List<int>();
- for (int i = 0; i < 100; i++)
- {
- manList.Add(new Man { Age = i + 15 });
- intList.Add(i+15);
- }
- for (int i = 0; i < 5; i++) { test.LinqStart(manList); Console.WriteLine(i + " test man performed"); }
- for (int i = 0; i < 5; i++) { test.LinqStart(intList); Console.WriteLine(i + " test int performed"); }
- Console.Write("press either key for the results");
- Console.ReadKey();
- Console.WriteLine();
- Console.WriteLine();
- test.PrintResults();
- Console.ReadKey();
- }
- }
- }
- /*
- Results for Man:
- Jit compiled time:
- Linq: 1 ms
- Linq: 3484 ticks
- Calculate: 1 ms
- Calculate: 2148 tick
- Avg after Jit time:
- Linq: 0 ms
- Linq: 21 ticks
- Calculate: 0 ms
- Calculate: 106 ticks
- Results for Int32:
- Jit compiled time:
- Linq: 10 ms
- Linq: 23855 ticks
- Calculate: 1 ms
- Calculate: 3679 tick
- Avg after Jit time:
- Linq: 0 ms
- Linq: 20 ticks
- Calculate: 0 ms
- Calculate: 44 ticks
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement