namespace GenericNumerics
{
using System;
using System.Collections.Generic;
//The interface for calculations.
//This will normally contain much more methods,
// but for the benchmark we have just Add
public interface ICalculator<T>
{
T Add(T a, T b);
}
//You would have to do something similar to this
//for each type you want to use in your list.
public struct DoubleCalculator : ICalculator<double>
{
public double Add(double a, double b) { return a + b; }
}
public class Test
{
//create a large List<double> for benchmarking
static List<double> list;
static Test()
{
list = new List<double>(1000000);
for (int i = 0; i < 1000000; i++)
list.Add(i);
}
//the non-generic sum method
public static double Sum(List<double> list)
{
double sum = 0;
for (int i = 0; i < list.Count; i++)
sum += list[i];
return sum;
}
//the generic sum method. Note the second type parameter.
public static T Sum<T, C>(List<T> list)
where T : new()
where C : ICalculator<T>, new()
{
C calculator = new C();
T sum = new T();
for (int i = 0; i < list.Count; i++)
sum = calculator.Add(sum, list[i]);
return sum;
}
public static void Main()
{
#if(DEBUG)
Console.WriteLine("Warning: Benchmarking in debug mode will not produce meaningful results!");
#endif
DateTime time0;
time0 = DateTime.Now;
for (int i = 0; i < 100; i++)
Sum(list);
Console.WriteLine("Non-generic sum took {0}.", (DateTime.Now - time0));
time0 = DateTime.Now;
for (int i = 0; i < 100; i++)
Sum<double, DoubleCalculator>(list);
Console.WriteLine("Generic sum took {0}.", (DateTime.Now - time0));
Console.ReadLine();
}
}
}
{
using System;
using System.Collections.Generic;
//The interface for calculations.
//This will normally contain much more methods,
// but for the benchmark we have just Add
public interface ICalculator<T>
{
T Add(T a, T b);
}
//You would have to do something similar to this
//for each type you want to use in your list.
public struct DoubleCalculator : ICalculator<double>
{
public double Add(double a, double b) { return a + b; }
}
public class Test
{
//create a large List<double> for benchmarking
static List<double> list;
static Test()
{
list = new List<double>(1000000);
for (int i = 0; i < 1000000; i++)
list.Add(i);
}
//the non-generic sum method
public static double Sum(List<double> list)
{
double sum = 0;
for (int i = 0; i < list.Count; i++)
sum += list[i];
return sum;
}
//the generic sum method. Note the second type parameter.
public static T Sum<T, C>(List<T> list)
where T : new()
where C : ICalculator<T>, new()
{
C calculator = new C();
T sum = new T();
for (int i = 0; i < list.Count; i++)
sum = calculator.Add(sum, list[i]);
return sum;
}
public static void Main()
{
#if(DEBUG)
Console.WriteLine("Warning: Benchmarking in debug mode will not produce meaningful results!");
#endif
DateTime time0;
time0 = DateTime.Now;
for (int i = 0; i < 100; i++)
Sum(list);
Console.WriteLine("Non-generic sum took {0}.", (DateTime.Now - time0));
time0 = DateTime.Now;
for (int i = 0; i < 100; i++)
Sum<double, DoubleCalculator>(list);
Console.WriteLine("Generic sum took {0}.", (DateTime.Now - time0));
Console.ReadLine();
}
}
}


