Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

<ItemGroup>
<Content Include="BenchmarkTextFile.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace FastestWayToReadATextFileInCsharp;

public class WaysToReadATextFileInCsharp
public class WaysToReadATextFileInCsharp(string sampleFilePath)
{
private readonly string _sampleFilePath;

public WaysToReadATextFileInCsharp(string sampleFilePath)
{
_sampleFilePath = sampleFilePath;
}
private readonly string _sampleFilePath = sampleFilePath;

public string UseFileReadAllLines()
{
Expand Down Expand Up @@ -132,11 +127,13 @@ public string UseBufferedStreamObjectWithNoFileStreamBuffer()
{
var stringBuilder = new StringBuilder();

using var fileStream = new FileStream(_sampleFilePath,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
0);
using var fileStream = new FileStream(_sampleFilePath, new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.Read,
BufferSize = 0
});
using var bufferedStream = new BufferedStream(fileStream);
using var streamReader = new StreamReader(bufferedStream);

Expand All @@ -148,4 +145,34 @@ public string UseBufferedStreamObjectWithNoFileStreamBuffer()

return stringBuilder.ToString();
}

public async Task<string> UseFileReadAllTextAsync()
=> await File.ReadAllTextAsync(_sampleFilePath);

public async Task<string> UseFileReadLinesAsync()
{
var stringBuilder = new StringBuilder();

await foreach (var line in File.ReadLinesAsync(_sampleFilePath))
{
stringBuilder.AppendLine(line);
}
stringBuilder.Length -= Environment.NewLine.Length;

return stringBuilder.ToString();
}

public async Task<string> UseStreamReaderReadToEndAsync()
{
using var fileStream = new FileStream(_sampleFilePath, new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.Read,
Options = FileOptions.Asynchronous | FileOptions.SequentialScan
});
using var streamReader = new StreamReader(fileStream);

return await streamReader.ReadToEndAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace FastestWayToReadATextFileInCsharp;
public class WaysToReadATextFileInCsharpBenchmark
{
private static readonly string SampleFilePath
= Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.Parent.Parent.FullName, "BenchmarkTextFile.txt");
= Path.Combine(AppContext.BaseDirectory, "BenchmarkTextFile.txt");

private readonly WaysToReadATextFileInCsharp _textFileReader = new(SampleFilePath);

Expand Down Expand Up @@ -53,4 +53,16 @@ public string BufferedStreamObject()
[Benchmark]
public string BufferedStreamWithNoFileStreamBuffer()
=> _textFileReader.UseBufferedStreamObjectWithNoFileStreamBuffer();

[Benchmark]
public async Task<string> FileReadAllTextAsync()
=> await _textFileReader.UseFileReadAllTextAsync();

[Benchmark]
public async Task<string> FileReadLinesAsync()
=> await _textFileReader.UseFileReadLinesAsync();

[Benchmark]
public async Task<string> StreamReaderReadToEndAsync()
=> await _textFileReader.UseStreamReaderReadToEndAsync();
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ public void WhenReadingATextFileWithBufferedStreamObjectWithFileStreamBufferDisa
Assert.Equal(ExpectedText, result);
}

[Fact]
public async Task WhenReadingATextFileWithFileReadAllTextAsync_ThenReturnsExpectedText()
{
var result = await _textFileReader.UseFileReadAllTextAsync();

Assert.Equal(ExpectedText, result);
}

[Fact]
public async Task WhenReadingATextFileWithFileReadLinesAsync_ThenReturnsExpectedText()
{
var result = await _textFileReader.UseFileReadLinesAsync();

Assert.Equal(ExpectedText, result);
}

[Fact]
public async Task WhenReadingATextFileWithStreamReaderReadToEndAsync_ThenReturnsExpectedText()
{
var result = await _textFileReader.UseStreamReaderReadToEndAsync();

Assert.Equal(ExpectedText, result);
}

public void Dispose()
{
if (File.Exists(TempFilePath))
Expand Down
Loading