diff --git a/Source/Benchmark/Benchmarks/Benchmark.cs b/Source/Benchmark/Benchmarks/Benchmark.cs index 7128090..115ca2e 100644 --- a/Source/Benchmark/Benchmarks/Benchmark.cs +++ b/Source/Benchmark/Benchmarks/Benchmark.cs @@ -11,8 +11,11 @@ protected Benchmark(int iterations) _iterations = iterations; } + protected abstract string Name { get; } + public void Measure() { + Console.WriteLine("Benchmark: {0}", Name); Console.WriteLine("Iterations: {0}", _iterations); TimeSpan handmade = MeasureHandmade(); diff --git a/Source/Benchmark/Benchmarks/CollectionBenchmark.cs b/Source/Benchmark/Benchmarks/CollectionBenchmark.cs index 61835e3..6006899 100644 --- a/Source/Benchmark/Benchmarks/CollectionBenchmark.cs +++ b/Source/Benchmark/Benchmarks/CollectionBenchmark.cs @@ -1,4 +1,8 @@ using System; +using System.Collections.Generic; +using System.Diagnostics; +using AutoMapper; +using Nelibur.ObjectMapper; namespace Benchmark.Benchmarks { @@ -6,21 +10,94 @@ public sealed class CollectionBenchmark : Benchmark { public CollectionBenchmark(int iterations) : base(iterations) { + InitMappers(); + } + + protected override string Name + { + get { return "Collection"; } } protected override TimeSpan MeasureAutoMapper() { - throw new NotImplementedException(); + Class1 source = CreateSource(); + Mapper.Map(source); + + Stopwatch stopwatch = Stopwatch.StartNew(); + + for (int i = 0; i < _iterations; i++) + { + var target = Mapper.Map(source); + } + stopwatch.Stop(); + return stopwatch.Elapsed; } protected override TimeSpan MeasureHandmade() { - throw new NotImplementedException(); + Class1 source = CreateSource(); + + Stopwatch stopwatch = Stopwatch.StartNew(); + + for (int i = 0; i < _iterations; i++) + { + var target = new Class2(); + target = HandmadeMap(source, target); + } + + stopwatch.Stop(); + return stopwatch.Elapsed; } protected override TimeSpan MeasureTinyMapper() { - throw new NotImplementedException(); + Class1 source = CreateSource(); + TinyMapper.Map(source); + + Stopwatch stopwatch = Stopwatch.StartNew(); + + for (int i = 0; i < _iterations; i++) + { + var target = TinyMapper.Map(source); + } + stopwatch.Stop(); + return stopwatch.Elapsed; + } + + private static Class1 CreateSource() + { + return new Class1 + { + Ints = new List { 1, 2, 3, 4, 5 } + }; + } + + private static void InitMappers() + { + TinyMapper.Bind(); + Mapper.CreateMap(); + } + + private Class2 HandmadeMap(Class1 source, Class2 target) + { + target.Ints = new List(); + foreach (int item in source.Ints) + { + target.Ints.Add(item); + } + return target; + } + + + public class Class1 + { + public List Ints { get; set; } + } + + + public class Class2 + { + public List Ints { get; set; } } } } diff --git a/Source/Benchmark/Benchmarks/PrimitiveTypeBenchmark.cs b/Source/Benchmark/Benchmarks/PrimitiveTypeBenchmark.cs index 40818ca..31c4f5f 100644 --- a/Source/Benchmark/Benchmarks/PrimitiveTypeBenchmark.cs +++ b/Source/Benchmark/Benchmarks/PrimitiveTypeBenchmark.cs @@ -12,6 +12,11 @@ public PrimitiveTypeBenchmark(int iterations) : base(iterations) InitMappers(); } + protected override string Name + { + get { return "PrimitiveType"; } + } + protected override TimeSpan MeasureAutoMapper() { Class1 source = CreateSource(); @@ -101,41 +106,41 @@ private static void InitMappers() TinyMapper.Bind(); Mapper.CreateMap(); } - } - public class Class1 - { - public bool Bool { get; set; } - public byte Byte { get; set; } - public char Char { get; set; } - public DateTime DateTime { get; set; } - public decimal Decimal { get; set; } - public string Email { get; set; } - public string FirstName { get; set; } - public float Float { get; set; } - public int Int { get; set; } - public string LastName { get; set; } - public long Long { get; set; } - public string Nickname { get; set; } - public short Short { get; set; } - } + public class Class1 + { + public bool Bool { get; set; } + public byte Byte { get; set; } + public char Char { get; set; } + public DateTime DateTime { get; set; } + public decimal Decimal { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public float Float { get; set; } + public int Int { get; set; } + public string LastName { get; set; } + public long Long { get; set; } + public string Nickname { get; set; } + public short Short { get; set; } + } - public class Class2 - { - public bool Bool { get; set; } - public byte Byte { get; set; } - public char Char { get; set; } - public DateTime DateTime { get; set; } - public decimal Decimal { get; set; } - public string Email { get; set; } - public string FirstName { get; set; } - public float Float { get; set; } - public int Int { get; set; } - public string LastName { get; set; } - public long Long { get; set; } - public string Nickname { get; set; } - public short Short { get; set; } + public class Class2 + { + public bool Bool { get; set; } + public byte Byte { get; set; } + public char Char { get; set; } + public DateTime DateTime { get; set; } + public decimal Decimal { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public float Float { get; set; } + public int Int { get; set; } + public string LastName { get; set; } + public long Long { get; set; } + public string Nickname { get; set; } + public short Short { get; set; } + } } } diff --git a/Source/Benchmark/Program.cs b/Source/Benchmark/Program.cs index 85464fe..ab3daeb 100644 --- a/Source/Benchmark/Program.cs +++ b/Source/Benchmark/Program.cs @@ -12,6 +12,9 @@ private static void Main() var primitiveTypeBenchmark = new PrimitiveTypeBenchmark(Iterations); primitiveTypeBenchmark.Measure(); + var collectionBenchmark = new CollectionBenchmark(Iterations); + collectionBenchmark.Measure(); + Console.WriteLine("Press any key to Exit"); Console.ReadLine(); } diff --git a/Source/TinyMapper/Mappers/Collections/CollectionMapper.cs b/Source/TinyMapper/Mappers/Collections/CollectionMapper.cs index 43a931d..fe04f64 100644 --- a/Source/TinyMapper/Mappers/Collections/CollectionMapper.cs +++ b/Source/TinyMapper/Mappers/Collections/CollectionMapper.cs @@ -33,6 +33,11 @@ protected virtual TTarget EnumerableToList(IEnumerable source) throw new NotImplementedException(); } + protected virtual TTarget EnumerableToList(IEnumerable source) + { + throw new NotImplementedException(); + } + protected List EnumerableToListTemplate(IEnumerable source) { var result = new List(); @@ -44,6 +49,11 @@ protected List EnumerableToListTemplate(IEnumerable so } protected override TTarget MapCore(TSource source, TTarget target) + { + return MapCoreDefault(source); + } + + private TTarget MapCoreDefault(TSource source) { Type targetType = typeof(TTarget); var enumerable = (IEnumerable)source; diff --git a/Source/TinyMapper/Mappers/Collections/CollectionMapperBuilder.cs b/Source/TinyMapper/Mappers/Collections/CollectionMapperBuilder.cs index 560daa6..0b590ce 100644 --- a/Source/TinyMapper/Mappers/Collections/CollectionMapperBuilder.cs +++ b/Source/TinyMapper/Mappers/Collections/CollectionMapperBuilder.cs @@ -33,7 +33,10 @@ protected override Mapper BuildCore(TypePair typePair) { Type parentType = typeof(CollectionMapper<,>).MakeGenericType(typePair.Source, typePair.Target); TypeBuilder typeBuilder = _assembly.DefineType(GetMapperFullName(), parentType); - if (IsIEnumerableToList(typePair)) + if (IsIEnumerableOfToList(typePair)) + { + } + else if (IsIEnumerableToList(typePair)) { EmitEnumerableToList(parentType, typeBuilder, typePair); } @@ -64,6 +67,11 @@ private static Type GetCollectionItemType(Type type) throw new NotSupportedException(); } + private static bool IsIEnumerableOfToList(TypePair typePair) + { + return typePair.Target.IsListOf() && typePair.Source.IsIEnumerableOf(); + } + private static bool IsIEnumerableToList(TypePair typePair) { return typePair.Source.IsIEnumerable() && typePair.Target.IsListOf();