Skip to content

Commit

Permalink
feat: add Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Oct 29, 2023
1 parent d5001ff commit e739a39
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 15 deletions.
150 changes: 150 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,156 @@ Options:
-mem check in-memory compression speed
```

## Benchmark
CPU : AMD Ryzen 5 5625U

| Name | Rate | MB/s |
|-------------------------------------------------------------------|-------|--------|
| K4os.Compression.LZ4 compression L00 - silesia/x-ray | 99.01 | 680.70 |
| K4os.Compression.LZ4 compression L00 - silesia/sao | 93.63 | 247.47 |
| K4os.Compression.LZ4 compression L00 - silesia/mr | 54.57 | 235.90 |
| K4os.Compression.LZ4 compression L00 - silesia/mozilla | 51.61 | 227.21 |
| K4os.Compression.LZ4 compression L00 - silesia/ooffice | 70.53 | 216.67 |
| K4os.Compression.LZ4 compression L00 - silesia/samba | 35.72 | 203.95 |
| K4os.Compression.LZ4 compression L00 - canterbury/xargs.1 | 62.95 | 202.69 |
| K4os.Compression.LZ4 compression L00 - canterbury/sum | 49.20 | 202.66 |
| K4os.Compression.LZ4 compression L00 - canterbury/kennedy.xls | 36.39 | 200.49 |
| K4os.Compression.LZ4 compression L00 - silesia/osdb | 52.12 | 189.70 |
| K4os.Compression.LZ4 compression L00 - enwik/enwik8 | 57.26 | 186.09 |
| K4os.Compression.LZ4 compression L00 - silesia/dickens | 63.07 | 183.56 |
| K4os.Compression.LZ4 compression L00 - canterbury/plrabn12.txt | 67.57 | 181.00 |
| K4os.Compression.LZ4 compression L00 - silesia/reymont | 48.01 | 174.78 |
| K4os.Compression.LZ4 compression L00 - silesia/webster | 48.58 | 173.96 |
| K4os.Compression.LZ4 compression L00 - silesia/xml | 22.96 | 172.04 |
| K4os.Compression.LZ4 compression L00 - silesia/nci | 16.49 | 166.65 |
| K4os.Compression.LZ4 compression L00 - canterbury/ptt5 | 16.93 | 162.88 |
| K4os.Compression.LZ4 compression L00 - canterbury/asyoulik.txt | 63.64 | 140.25 |
| DotFastLZ.Compression compression L2 - canterbury/kennedy.xls | 40.08 | 115.64 |
| DotFastLZ.Compression compression L1 - canterbury/kennedy.xls | 39.37 | 114.79 |
| K4os.Compression.LZ4 compression L00 - canterbury/lcet10.txt | 54.65 | 111.52 |
| DotFastLZ.Compression compression L2 - silesia/sao | 88.07 | 109.26 |
| DotFastLZ.Compression compression L2 - silesia/x-ray | 96.70 | 107.14 |
| DotFastLZ.Compression compression L2 - silesia/osdb | 52.65 | 98.09 |
| DotFastLZ.Compression compression L1 - silesia/x-ray | 96.74 | 96.93 |
| DotFastLZ.Compression compression L1 - silesia/mr | 50.89 | 96.64 |
| DotFastLZ.Compression compression L2 - silesia/mozilla | 51.66 | 93.46 |
| DotFastLZ.Compression compression L2 - silesia/mr | 50.59 | 91.63 |
| K4os.Compression.LZ4 compression L00 - canterbury/cp.html | 48.40 | 90.26 |
| DotFastLZ.Compression compression L1 - silesia/mozilla | 52.69 | 90.25 |
| DotFastLZ.Compression compression L1 - silesia/osdb | 65.83 | 89.04 |
| DotFastLZ.Compression compression L2 - silesia/samba | 35.45 | 87.08 |
| DotFastLZ.Compression compression L2 - canterbury/sum | 49.40 | 86.64 |
| DotFastLZ.Compression compression L2 - silesia/ooffice | 68.78 | 85.58 |
| DotFastLZ.Compression compression L1 - silesia/samba | 38.61 | 85.04 |
| DotFastLZ.Compression compression L1 - silesia/nci | 20.64 | 84.64 |
| DotFastLZ.Compression compression L1 - silesia/dickens | 59.46 | 83.57 |
| DotFastLZ.Compression compression L2 - silesia/dickens | 58.84 | 83.14 |
| DotFastLZ.Compression compression L1 - silesia/reymont | 49.85 | 83.10 |
| DotFastLZ.Compression compression L1 - silesia/sao | 88.08 | 82.99 |
| DotFastLZ.Compression compression L1 - silesia/xml | 25.97 | 82.79 |
| DotFastLZ.Compression compression L2 - silesia/nci | 19.60 | 82.71 |
| DotFastLZ.Compression compression L2 - silesia/reymont | 47.60 | 82.70 |
| DotFastLZ.Compression compression L2 - canterbury/plrabn12.txt | 61.85 | 82.58 |
| DotFastLZ.Compression compression L1 - enwik/enwik8 | 55.58 | 81.72 |
| DotFastLZ.Compression compression L1 - silesia/ooffice | 69.48 | 81.44 |
| DotFastLZ.Compression compression L1 - silesia/webster | 48.91 | 81.00 |
| DotFastLZ.Compression compression L2 - canterbury/lcet10.txt | 53.70 | 80.78 |
| DotFastLZ.Compression compression L2 - silesia/webster | 47.89 | 80.43 |
| DotFastLZ.Compression compression L2 - enwik/enwik8 | 54.52 | 79.77 |
| DotFastLZ.Compression compression L2 - silesia/xml | 23.90 | 78.78 |
| DotFastLZ.Compression compression L1 - canterbury/sum | 53.66 | 78.48 |
| DotFastLZ.Compression compression L2 - canterbury/xargs.1 | 58.46 | 77.26 |
| K4os.Compression.LZ4 compression L00 - canterbury/fields.c | 46.80 | 76.82 |
| DotFastLZ.Compression compression L1 - canterbury/ptt5 | 15.84 | 72.88 |
| DotFastLZ.Compression compression L1 - canterbury/plrabn12.txt | 62.37 | 70.88 |
| DotFastLZ.Compression compression L1 - canterbury/lcet10.txt | 54.67 | 68.25 |
| DotFastLZ.Compression compression L2 - canterbury/ptt5 | 15.77 | 67.82 |
| DotFastLZ.Compression compression L2 - canterbury/cp.html | 47.77 | 60.61 |
| DotFastLZ.Compression compression L1 - canterbury/xargs.1 | 58.46 | 58.91 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/xargs.1 | 57.09 | 58.85 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/xargs.1 | 57.23 | 54.74 |
| DotFastLZ.Compression compression L1 - canterbury/fields.c | 42.46 | 51.63 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/grammar.lsp | 46.63 | 46.24 |
| DotFastLZ.Compression compression L1 - canterbury/cp.html | 49.32 | 46.14 |
| DotFastLZ.Compression compression L2 - canterbury/fields.c | 42.38 | 45.13 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/sum | 43.44 | 44.80 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/grammar.lsp | 46.41 | 43.43 |
| DotFastLZ.Compression compression L1 - canterbury/asyoulik.txt | 59.54 | 42.33 |
| K4os.Compression.LZ4 compression L00 - canterbury/alice29.txt | 58.32 | 39.19 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/kennedy.xls | 31.96 | 38.89 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/fields.c | 38.27 | 38.65 |
| DotFastLZ.Compression compression L2 - canterbury/grammar.lsp | 47.89 | 38.36 |
| K4os.Compression.LZ4 compression L03_HC - silesia/mr | 46.59 | 38.02 |
| K4os.Compression.LZ4 compression L03_HC - silesia/osdb | 40.11 | 36.99 |
| K4os.Compression.LZ4 compression L03_HC - silesia/x-ray | 84.99 | 36.39 |
| K4os.Compression.LZ4 compression L03_HC - silesia/mozilla | 44.15 | 36.38 |
| DotFastLZ.Compression compression L2 - canterbury/asyoulik.txt | 58.91 | 35.81 |
| K4os.Compression.LZ4 compression L03_HC - silesia/sao | 80.96 | 35.57 |
| K4os.Compression.LZ4 compression L03_HC - silesia/ooffice | 58.64 | 35.01 |
| DotFastLZ.Compression compression L1 - canterbury/alice29.txt | 56.19 | 33.97 |
| K4os.Compression.LZ4 compression L09_HC - silesia/x-ray | 84.67 | 33.10 |
| DotFastLZ.Compression compression L1 - canterbury/grammar.lsp | 47.89 | 32.94 |
| K4os.Compression.LZ4 compression L03_HC - silesia/samba | 29.20 | 32.02 |
| K4os.Compression.LZ4 compression L03_HC - enwik/enwik8 | 43.83 | 31.95 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/asyoulik.txt | 49.55 | 30.91 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/lcet10.txt | 41.21 | 30.87 |
| K4os.Compression.LZ4 compression L03_HC - silesia/dickens | 46.87 | 29.96 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/plrabn12.txt | 50.64 | 29.39 |
| K4os.Compression.LZ4 compression L03_HC - silesia/reymont | 36.64 | 28.57 |
| K4os.Compression.LZ4 compression L03_HC - silesia/webster | 35.55 | 28.27 |
| DotFastLZ.Compression compression L2 - canterbury/alice29.txt | 55.68 | 27.39 |
| K4os.Compression.LZ4 compression L03_HC - silesia/xml | 15.95 | 26.24 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/cp.html | 42.03 | 25.89 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/ptt5 | 13.55 | 25.33 |
| K4os.Compression.LZ4 compression L03_HC - silesia/nci | 12.67 | 23.70 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/fields.c | 37.95 | 22.58 |
| K4os.Compression.LZ4 compression L12 - silesia/x-ray | 84.64 | 22.51 |
| K4os.Compression.LZ4 compression L09_HC - silesia/sao | 79.09 | 20.84 |
| K4os.Compression.LZ4 compression L12 - canterbury/xargs.1 | 56.87 | 20.53 |
| K4os.Compression.LZ4 compression L09_HC - silesia/osdb | 39.44 | 20.43 |
| K4os.Compression.LZ4 compression L09_HC - silesia/ooffice | 57.60 | 19.82 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/sum | 42.72 | 17.61 |
| K4os.Compression.LZ4 compression L09_HC - silesia/mozilla | 43.11 | 15.98 |
| K4os.Compression.LZ4 compression L12 - canterbury/grammar.lsp | 46.25 | 14.82 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/cp.html | 42.47 | 14.00 |
| K4os.Compression.LZ4 compression L12 - silesia/sao | 78.17 | 13.91 |
| K4os.Compression.LZ4 compression L09_HC - enwik/enwik8 | 42.20 | 13.60 |
| K4os.Compression.LZ4 compression L09_HC - silesia/samba | 28.42 | 12.30 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/asyoulik.txt | 47.06 | 11.98 |
| K4os.Compression.LZ4 compression L12 - canterbury/cp.html | 41.83 | 11.70 |
| K4os.Compression.LZ4 compression L12 - silesia/osdb | 39.13 | 10.29 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/lcet10.txt | 38.85 | 10.15 |
| K4os.Compression.LZ4 compression L09_HC - silesia/webster | 33.77 | 10.07 |
| K4os.Compression.LZ4 compression L12 - canterbury/fields.c | 37.71 | 9.61 |
| K4os.Compression.LZ4 compression L12 - silesia/ooffice | 57.46 | 9.39 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/plrabn12.txt | 47.20 | 9.15 |
| K4os.Compression.LZ4 compression L09_HC - silesia/dickens | 43.49 | 9.08 |
| K4os.Compression.LZ4 compression L09_HC - silesia/xml | 14.41 | 8.40 |
| K4os.Compression.LZ4 compression L09_HC - silesia/mr | 42.58 | 8.23 |
| K4os.Compression.LZ4 compression L12 - enwik/enwik8 | 41.91 | 6.96 |
| K4os.Compression.LZ4 compression L12 - canterbury/plrabn12.txt | 46.62 | 5.93 |
| K4os.Compression.LZ4 compression L12 - canterbury/lcet10.txt | 38.46 | 5.75 |
| K4os.Compression.LZ4 compression L12 - silesia/dickens | 42.93 | 5.55 |
| K4os.Compression.LZ4 compression L12 - canterbury/sum | 42.60 | 5.28 |
| K4os.Compression.LZ4 compression L03_HC - canterbury/alice29.txt | 44.64 | 5.10 |
| K4os.Compression.LZ4 compression L12 - silesia/webster | 33.34 | 5.04 |
| K4os.Compression.LZ4 compression L12 - canterbury/asyoulik.txt | 46.58 | 4.89 |
| K4os.Compression.LZ4 compression L09_HC - silesia/reymont | 31.86 | 4.81 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/kennedy.xls | 31.48 | 4.70 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/ptt5 | 13.01 | 4.60 |
| K4os.Compression.LZ4 compression L12 - silesia/mr | 42.02 | 3.88 |
| K4os.Compression.LZ4 compression L09_HC - silesia/nci | 10.95 | 3.83 |
| K4os.Compression.LZ4 compression L12 - silesia/samba | 28.21 | 3.59 |
| K4os.Compression.LZ4 compression L12 - silesia/xml | 14.22 | 3.48 |
| K4os.Compression.LZ4 compression L12 - silesia/reymont | 31.13 | 3.11 |
| K4os.Compression.LZ4 compression L12 - silesia/mozilla | 42.98 | 2.89 |
| K4os.Compression.LZ4 compression L00 - canterbury/grammar.lsp | 51.46 | 2.67 |
| K4os.Compression.LZ4 compression L09_HC - canterbury/alice29.txt | 41.87 | 2.47 |
| K4os.Compression.LZ4 compression L12 - silesia/nci | 10.78 | 1.92 |
| K4os.Compression.LZ4 compression L12 - canterbury/alice29.txt | 41.40 | 1.74 |
| K4os.Compression.LZ4 compression L12 - canterbury/ptt5 | 12.89 | 1.01 |
| K4os.Compression.LZ4 compression L12 - canterbury/kennedy.xls | 31.48 | 0.29 |

## Overview

FastLZ (MIT license) is an ANSI C/C90 implementation of [Lempel-Ziv 77 algorithm](https://en.wikipedia.org/wiki/LZ77_and_LZ78#LZ77) (LZ77) of lossless data compression. It is suitable to compress series of text/paragraphs, sequences of raw pixel data, or any other blocks of data with lots of repetition. It is not intended to be used on images, videos, and other formats of data typically already in an optimal compressed form.
Expand Down
50 changes: 47 additions & 3 deletions src/DotFastLZ.Benchmark/BenchmarkResult.cs
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;
}
}
6 changes: 2 additions & 4 deletions src/DotFastLZ.Benchmark/DotFastLZ.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@

<ItemGroup>
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.6" />
</ItemGroup>

<ItemGroup>
<Folder Include="DotFastLZ.Resource\" />
<PackageReference Include="EasyCompressor.LZ4" Version="1.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DotFastLZ.Resource\DotFastLZ.Resource.csproj" />
<ProjectReference Include="..\DotFastLZ.Compression\DotFastLZ.Compression.csproj" />
</ItemGroup>

</Project>
90 changes: 90 additions & 0 deletions src/DotFastLZ.Benchmark/FixedArrayBufferWriter.cs
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})");
}
}
Loading

0 comments on commit e739a39

Please sign in to comment.