From 3856a722cfaaca931241eb35e90564b50fc25986 Mon Sep 17 00:00:00 2001 From: Amy Date: Wed, 2 Oct 2024 13:56:01 +0100 Subject: [PATCH 1/2] feat: added new benchmarks for comparison to manual mapping --- MapDataReader.Benchmarks/Program.cs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/MapDataReader.Benchmarks/Program.cs b/MapDataReader.Benchmarks/Program.cs index d402e0c..dd18ef9 100644 --- a/MapDataReader.Benchmarks/Program.cs +++ b/MapDataReader.Benchmarks/Program.cs @@ -63,12 +63,32 @@ public void MapDatareader_ViaDapper() } [Benchmark] - public void MapDataReader_ViaMapaDataReader() + public void MapDataReader_ViaMapDataReader() { var dr = _dt.CreateDataReader(); var list = dr.ToTestClass(); } + [Benchmark] + public void MapDataReader_ViaManualMap() + { + var dr = _dt.CreateDataReader(); + + var list = new List(); + while (dr.Read()) + { + list.Add(new TestClass + { + String1 = dr["String1"] as string, + String2 = dr["String2"] as string, + String3 = dr["String3"] as string, + Int = dr.GetInt32(3), + Int2 = dr.GetInt32(4), + IntNullable = dr["IntNullable"] as int? + }); + } + } + static DataTable _dt; [GlobalSetup] @@ -99,8 +119,8 @@ public class TestClass public string String1 { get; set; } public string String2 { get; set; } public string String3 { get; set; } - public string Int { get; set; } - public string Int2 { get; set; } + public int Int { get; set; } + public int Int2 { get; set; } public int? IntNullable { get; set; } } } \ No newline at end of file From 0b5b9a2319f54fe0a171ad0f11dc41cd88c1b962 Mon Sep 17 00:00:00 2001 From: Amy Date: Sun, 6 Oct 2024 15:37:24 +0100 Subject: [PATCH 2/2] feat: updated the benchmarks to show `cast`, `as`, and `get` versions --- MapDataReader.Benchmarks/Program.cs | 44 +++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/MapDataReader.Benchmarks/Program.cs b/MapDataReader.Benchmarks/Program.cs index dd18ef9..898095e 100644 --- a/MapDataReader.Benchmarks/Program.cs +++ b/MapDataReader.Benchmarks/Program.cs @@ -70,7 +70,27 @@ public void MapDataReader_ViaMapDataReader() } [Benchmark] - public void MapDataReader_ViaManualMap() + public void MapDataReader_ViaManualMap_CastMethods() + { + var dr = _dt.CreateDataReader(); + + var list = new List(); + while (dr.Read()) + { + list.Add(new TestClass + { + String1 = (string)dr["String1"], + String2 = (string)dr["String2"], + String3 = (string)dr["String3"], + Int = (int)dr["Int"], + Int2 = (int)dr["Int2"], + IntNullable = (int?)dr["IntNullable"] + }); + } + } + + [Benchmark] + public void MapDataReader_ViaManualMap_AsMethods() { var dr = _dt.CreateDataReader(); @@ -82,9 +102,29 @@ public void MapDataReader_ViaManualMap() String1 = dr["String1"] as string, String2 = dr["String2"] as string, String3 = dr["String3"] as string, + Int = dr["Int"] as int? ?? 0, + Int2 = dr["Int2"] as int? ?? 0, + IntNullable = dr["IntNullable"] as int? + }); + } + } + + [Benchmark] + public void MapDataReader_ViaManualMap_GetMethods() + { + var dr = _dt.CreateDataReader(); + + var list = new List(); + while (dr.Read()) + { + list.Add(new TestClass + { + String1 = dr.GetString(0), + String2 = dr.GetString(1), + String3 = dr.GetString(2), Int = dr.GetInt32(3), Int2 = dr.GetInt32(4), - IntNullable = dr["IntNullable"] as int? + IntNullable = dr.GetInt32(5) // this wouldn't work if the value is null though, this is just for benchmarking }); } }