-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
703 additions
and
1 deletion.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
CsvHelper.FastDynamic.Performance/CsvHelper.FastDynamic.Performance.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CsvHelper.FastDynamic\CsvHelper.FastDynamic.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="sampledata\SFO_Airport_Monthly_Utility_Consumption_for_Natural_Gas__Water__and_Electricity.csv"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
36 changes: 36 additions & 0 deletions
36
CsvHelper.FastDynamic.Performance/Internal/CsvReaderExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace CsvHelper.FastDynamic.Performance.Internal | ||
{ | ||
internal static class CsvReaderExtension | ||
{ | ||
internal static IList<IDictionary<string, object>> GetDictionaryRecords(this CsvReader csvReader) | ||
{ | ||
// Read Header | ||
csvReader.Read(); | ||
csvReader.ReadHeader(); | ||
|
||
var headerRecord = csvReader.Context | ||
.HeaderRecord | ||
.Select((x, i) => csvReader.Configuration.PrepareHeaderForMatch(x, i)) | ||
.ToArray(); | ||
|
||
var result = new List<IDictionary<string, object>>(); | ||
|
||
while (csvReader.Read()) | ||
{ | ||
var record = new Dictionary<string, object>(headerRecord.Length); | ||
|
||
for (int i = 0; i < headerRecord.Length; i++) | ||
{ | ||
record[headerRecord[i]] = csvReader[i]; | ||
} | ||
|
||
result.Add(record); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace CsvHelper.FastDynamic.Performance | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
BenchmarkRunner.Run<ReaderBenchmark>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
using CsvHelper.FastDynamic.Performance.Internal; | ||
|
||
namespace CsvHelper.FastDynamic.Performance | ||
{ | ||
[MemoryDiagnoser] | ||
public class ReaderBenchmark | ||
{ | ||
private const string SampleCsvFile = @".\sampledata\SFO_Airport_Monthly_Utility_Consumption_for_Natural_Gas__Water__and_Electricity.csv"; | ||
|
||
public ReaderBenchmark() | ||
{ | ||
_sampleCsvData = File.ReadAllText(SampleCsvFile); | ||
} | ||
|
||
private readonly string _sampleCsvData; | ||
|
||
[Benchmark] | ||
public IReadOnlyList<dynamic> GetRecords() | ||
{ | ||
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture); | ||
|
||
return csvReader.GetRecords<dynamic>().ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public IReadOnlyList<IDictionary<string, object>> GetDictionaryRecords() | ||
{ | ||
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture); | ||
|
||
return csvReader.GetDictionaryRecords().ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public IReadOnlyList<dynamic> GetDynamicRecords() | ||
{ | ||
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture); | ||
|
||
return csvReader.GetDynamicRecords(); | ||
} | ||
} | ||
} |
Oops, something went wrong.