Skip to content

Commit 0e0380a

Browse files
committed
Applied more optimizations as suggested by David.
1 parent 8573d0b commit 0e0380a

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/FileIO/WithPipeLines.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,18 @@ private static class LineParser
105105
case 3:
106106
{
107107
var buffer = line[..comaAt];
108-
if (Utf8Parser.TryParse(buffer, out DateTime value, out var bytesConsumed))
108+
if (DateTime.TryParse(Encoding.UTF8.GetString(line[..comaAt]), out var doj))
109+
109110
{
110-
record.DateOfJoining = value;
111+
record.DateOfJoining = doj;
111112
}
113+
// Can't use Utf8 parser to extract datetime field because csv format doesn't have time
114+
//https://docs.microsoft.com/en-us/dotnet/api/system.buffers.text.utf8parser.tryparse?view=net-5.0#System_Buffers_Text_Utf8Parser_TryParse_System_ReadOnlySpan_System_Byte__System_DateTime__System_Int32__System_Char_
115+
116+
// if (Utf8Parser.TryParse(buffer, out DateTime value, out var bytesConsumed))
117+
// {
118+
// record.DateOfJoining = value;
119+
// }
112120
break;
113121
}
114122

tests/FileIO.Benchmarks/FileIOTest.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Buffers;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Reflection;
45
using System.Threading.Tasks;
@@ -22,7 +23,8 @@ public void Setup()
2223
[Benchmark]
2324
public async Task PipeLines()
2425
{
25-
26+
// var directoryPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program))?.Location);
27+
//_filePath = Path.Combine(directoryPath ?? string.Empty, "Employees.csv");
2628
var pool = ArrayPool<Employee>.Shared;
2729
var employeeRecords = pool.Rent(100000);
2830
var pipeLinesTest = new WithPipeLines();
@@ -38,17 +40,20 @@ public async Task PipeLines()
3840
}
3941

4042
[Benchmark]
41-
public async Task AsyncStream()
43+
public async Task<IList<Employee>> AsyncStream()
4244
{
45+
// var directoryPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program))?.Location);
46+
// _filePath = Path.Combine(directoryPath ?? string.Empty, "Employees.csv");
4347
var asyncStream = new WithAsyncStreams();
4448
var employees = await asyncStream.ProcessStreamAsync(_filePath);
49+
return employees;
4550
}
4651

4752
[Benchmark]
4853
public void CsvHelper()
4954
{
50-
var directoryPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program))?.Location);
51-
_filePath = Path.Combine(directoryPath ?? string.Empty, "Employees.csv");
55+
// var directoryPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program))?.Location);
56+
//_filePath = Path.Combine(directoryPath ?? string.Empty, "Employees.csv");
5257
var csvHelper = new WithCsvHelperLib();
5358
var employeesList = csvHelper.ProcessFileAsync(_filePath);
5459

tests/FileIO.Benchmarks/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ static async Task Main(string[] args)
1111

1212
//var test = new FileIOTest();
1313
//await test.PipeLines();
14-
//await test.AsyncStream();
15-
//test.CsvHelper();
14+
// await test.AsyncStream();
15+
// test.CsvHelper();
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)