Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
namespace SimpleConsole
{
public static class CodeTimer
{
public static void Initialize()
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Time("", 1, () => { });
}
public static void Time(string name, int iteration, Action action)
{
if (String.IsNullOrEmpty(name)) return;
// warm up
action();
// 1.
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(name);
// 2.
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
int[] gcCounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
}
// 3.
Stopwatch watch = new Stopwatch();
watch.Start();
ulong cycleCount = GetCycleCount();
for (int i = 0; i < iteration; i++) action();
ulong cpuCycles = GetCycleCount() - cycleCount;
watch.Stop();
// 4.
Console.ForegroundColor = currentForeColor;
Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));
// 5.
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine("\tGen " + i + ": \t\t" + count);
}
Console.WriteLine();
}
private static ulong GetCycleCount()
{
ulong cycleCount = 0;
QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
return cycleCount;
}
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
}
}
namespace SimpleConsole
{
using System;
using System.Collections.Generic;
using System.Linq;
public class Person
{
public int ID;
public string FirstName;
public string LastName;
public int Age;
public string FirstName1;
public string LastName1;
public int Age1;
public string FirstName2;
public string LastName2;
public int Age2;
}
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
var random = new Random(DateTime.Now.Millisecond);
var array = Enumerable.Repeat(0, 1000 * 50).Select(_ => new Person { ID = random.Next() }).ToArray();
CodeTimer.Initialize();
CodeTimer.Time("SortWithCustomComparer", 100,
() => SortWithCustomComparer(CloneArray(array)));
CodeTimer.Time("SortWithLinq", 100,
() => SortWithLinq(CloneArray(array)));
Console.ReadLine();
}
static void SortWithCustomComparer(Person[] array)
{
Array.Sort(array, new PersonComparer());
}
static void SortWithLinq(Person[] array)
{
var sorted =
(from i in array
orderby i.ID
select i).ToList();
}
private class PersonComparer : IComparer<Person>
{
#region IComparer<Person> Members
public int Compare(Person x, Person y)
{
return x.ID - y.ID;
}
#endregion
}
static T[] CloneArray<T>(T[] source)
{
var dest = new T[source.Length];
Array.Copy(source, dest, source.Length);
return dest;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment