Consider the set of elements that are derived from the base element.
// "Abstract" general element class class AElement { public int Key; } // Specific element class implementation class BElement : AElement { }
I want to save to the list. Two options:
List<AElement> aData = new List<AElement>(); List<BElement> bData = new List<BElement>();
If you add BElement to the aData and bData lists and perform operations on them, the bData version is significantly faster than the aData version. For example, if you use a generic dump of BubbleSort, sorting the "key" AElement:
static void BubbleSort<TElement>(List<TElement> Data) where TElement : AElement { for (int i = 0; i < Data.Count; i++) { for (int j = 0; j < Data.Count; j++) { if (Data[i].Key< Data[j].Key) { TElement tmp = Data[i]; Data[i] = Data[j]; Data[j] = tmp; } } } }
In my case with 5000 data elements, I see a 20% difference in favor of bData compared to aData.
Why is bData faster than aData here?
Edit: Added full code:
using System; using System.Collections.Generic; using System.Diagnostics; namespace TemplateClassPerformance { class Program { // "Abstract" general element class class AElement { public int Index; } // Specific element class implementation class BElement : AElement { } static void Main(string[] args) { Random random = new Random(); Stopwatch stopwatch = new Stopwatch(); for (int j = 0; j < 10; j++) { List<AElement> aData = new List<AElement>(); List<BElement> bData = new List<BElement>(); // Put the same elements in both lists for (int i = 0; i < 5000; i++) { BElement element = new BElement(); element.Index = random.Next(1000000); aData.Add(element); bData.Add(element); } stopwatch.Reset(); stopwatch.Start(); BubbleSort(bData); stopwatch.Stop(); long sbTicks = stopwatch.ElapsedTicks; stopwatch.Reset(); stopwatch.Start(); BubbleSort(aData); stopwatch.Stop(); long saTicks = stopwatch.ElapsedTicks; Console.Out.WriteLine("sb: {0}, sa: {1}", sbTicks, saTicks); } } static void BubbleSort<TElement>(List<TElement> data) where TElement : AElement { for (int i = 0; i < data.Count; i++) { for (int j = 0; j < data.Count; j++) { if (data[i].Index < data[j].Index) { TElement tmp = data[i]; data[i] = data[j]; data[j] = tmp; } } } } } }
source share