-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
393 additions
and
15 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,8 +1,52 @@ | ||
namespace DotFastLZ.Benchmark; | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace DotFastLZ.Benchmark; | ||
|
||
public class BenchmarkResult | ||
{ | ||
public string Name; | ||
public int Times; | ||
public long StartTicks; | ||
public long EndTicks; | ||
public Stopwatch Watch; | ||
public long SourceBytes; | ||
public long DestBytes; | ||
|
||
public static double CalculateCompressionSpeed(long size, TimeSpan timeSpan) | ||
{ | ||
// MB/s | ||
return (double)size / (1024 * 1024) / timeSpan.TotalSeconds; | ||
} | ||
|
||
public double ComputeRate() | ||
{ | ||
return DestBytes / (double)SourceBytes * 100; | ||
} | ||
|
||
public string ToRateString() | ||
{ | ||
return $"{ComputeRate():F2}"; | ||
} | ||
|
||
public double ComputeSpeed() | ||
{ | ||
return CalculateCompressionSpeed(DestBytes, Watch.Elapsed); | ||
} | ||
|
||
public string ToSpeedString() | ||
{ | ||
return $"{ComputeSpeed():F2}"; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
var result = ""; | ||
result += $"{Name}\n"; | ||
result += $" - times: {Times}\n"; | ||
result += $" - source bytes: {SourceBytes / Times}\n"; | ||
result += $" - compression bytes: {DestBytes / Times}\n"; | ||
result += $" - compression rate: {ComputeRate():F2}%\n"; | ||
result += $" - compression speed: {ComputeSpeed():F2} MB/s\n"; | ||
|
||
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
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,90 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Diagnostics; | ||
|
||
namespace DotFastLZ.Benchmark; | ||
|
||
sealed class FixedArrayBufferWriter<T> : IBufferWriter<T> | ||
{ | ||
private const int ArrayMaxLength = 0x7FFFFFC7; | ||
private const int DefaultInitialBufferSize = 256; | ||
|
||
private readonly T[] _buffer; | ||
private int _index; | ||
|
||
public FixedArrayBufferWriter(T[] buffer) | ||
{ | ||
if (buffer.Length <= 0) | ||
throw new ArgumentException(null, nameof(buffer)); | ||
|
||
_buffer = buffer; | ||
_index = 0; | ||
} | ||
|
||
public ReadOnlyMemory<T> WrittenMemory => _buffer.AsMemory(0, _index); | ||
public ReadOnlySpan<T> WrittenSpan => _buffer.AsSpan(0, _index); | ||
public int WrittenCount => _index; | ||
public int Capacity => _buffer.Length; | ||
public int FreeCapacity => _buffer.Length - _index; | ||
|
||
public void Clear() | ||
{ | ||
Debug.Assert(_buffer.Length >= _index); | ||
_buffer.AsSpan(0, _index).Clear(); | ||
_index = 0; | ||
} | ||
|
||
public void Advance(int count) | ||
{ | ||
if (count < 0) | ||
throw new ArgumentException(null, nameof(count)); | ||
|
||
if (_index > _buffer.Length - count) | ||
ThrowInvalidOperationException_AdvancedTooFar(_buffer.Length); | ||
|
||
_index += count; | ||
} | ||
|
||
public Memory<T> GetMemory(int sizeHint = 0) | ||
{ | ||
CheckAndResizeBuffer(sizeHint); | ||
Debug.Assert(_buffer.Length > _index); | ||
return _buffer.AsMemory(_index); | ||
} | ||
|
||
public Span<T> GetSpan(int sizeHint = 0) | ||
{ | ||
CheckAndResizeBuffer(sizeHint); | ||
Debug.Assert(_buffer.Length > _index); | ||
return _buffer.AsSpan(_index); | ||
} | ||
|
||
private void CheckAndResizeBuffer(int sizeHint) | ||
{ | ||
if (sizeHint < 0) | ||
throw new ArgumentException(nameof(sizeHint)); | ||
|
||
if (sizeHint == 0) | ||
{ | ||
sizeHint = 1; | ||
} | ||
|
||
if (sizeHint > FreeCapacity) | ||
{ | ||
// Attempt to grow to ArrayMaxLength. | ||
int currentLength = _buffer.Length; | ||
uint needed = (uint)(currentLength - FreeCapacity + sizeHint); | ||
ThrowOutOfMemoryException(needed); | ||
} | ||
} | ||
|
||
private static void ThrowInvalidOperationException_AdvancedTooFar(int capacity) | ||
{ | ||
throw new InvalidOperationException($"BufferWriterAdvancedTooFar, {capacity})"); | ||
} | ||
|
||
private static void ThrowOutOfMemoryException(uint capacity) | ||
{ | ||
throw new OutOfMemoryException($"BufferMaximumSizeExceeded, {capacity})"); | ||
} | ||
} |
Oops, something went wrong.