BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.674)
11th Gen Intel Core i7-1185G7 3.00GHz, 1 CPU, 8 logical and 4 physical cores
[Host] : .NET Framework 4.8.1 (4.8.9032.0), X64 RyuJIT VectorSize=256
DefaultJob : .NET Framework 4.8.1 (4.8.9032.0), X64 RyuJIT VectorSize=256
| Method | Offset | Mean | Error | StdDev | Median | Ratio | Gen0 | Gen1 | Allocated | Alloc Ratio |
|---------- |------- |----------------:|--------------:|---------------:|----------------:|------:|--------:|-------:|----------:|------------:|
| Skip | -1 | 151,726.6506 ns | 3,007.3543 ns | 6,664.0894 ns | 150,450.1953 ns | 1.000 | 12.9395 | 0.9766 | 82566 B | 1.00 |
| BlockCopy | -1 | 0.1605 ns | 0.0581 ns | 0.1252 ns | 0.1152 ns | 0.000 | - | - | - | 0.00 |
| Segment | -1 | 10.6340 ns | 0.2361 ns | 0.4196 ns | 10.5082 ns | 0.000 | - | - | - | 0.00 |
| | | | | | | | | | | |
| Skip | 0 | 153,767.2458 ns | 3,049.1016 ns | 8,499.6765 ns | 152,686.4502 ns | 1.000 | 12.9395 | 0.9766 | 82566 B | 1.00 |
| BlockCopy | 0 | 0.1472 ns | 0.0587 ns | 0.1013 ns | 0.1120 ns | 0.000 | - | - | - | 0.00 |
| Segment | 0 | 13.7178 ns | 0.4738 ns | 1.3364 ns | 13.2508 ns | 0.000 | - | - | - | 0.00 |
| | | | | | | | | | | |
| Skip | 1 | 175,118.9780 ns | 5,931.9158 ns | 16,924.0895 ns | 171,513.3057 ns | 1.000 | 12.9395 | 0.9766 | 82566 B | 1.00 |
| BlockCopy | 1 | 792.9510 ns | 31.1743 ns | 91.4287 ns | 778.0930 ns | 0.005 | 2.6102 | 0.1373 | 16456 B | 0.20 |
| Segment | 1 | 14.3662 ns | 0.5083 ns | 1.4086 ns | 14.0977 ns | 0.000 | - | - | - | 0.00 |
| | | | | | | | | | | |
| Skip | 1024 | 164,130.9174 ns | 4,012.2425 ns | 11,703.8997 ns | 162,051.8921 ns | 1.000 | 7.5684 | 0.4883 | 48718 B | 1.00 |
| BlockCopy | 1024 | 796.9357 ns | 36.3113 ns | 106.4947 ns | 779.0838 ns | 0.005 | 2.4509 | 0.1163 | 15432 B | 0.32 |
| Segment | 1024 | 14.1748 ns | 0.7904 ns | 2.3180 ns | 13.6365 ns | 0.000 | - | - | - | 0.00 |
| | | | | | | | | | | |
| Skip | 16384 | 41,364.4897 ns | 1,298.4945 ns | 3,746.4536 ns | 40,894.0216 ns | 1.000 | - | - | 305 B | 1.00 |
| BlockCopy | 16384 | 13.9454 ns | 0.4991 ns | 1.4637 ns | 13.7183 ns | 0.000 | 0.0076 | - | 48 B | 0.16 |
| Segment | 16384 | 12.3645 ns | 0.4836 ns | 1.3874 ns | 12.0102 ns | 0.000 | - | - | - | 0.00 |
Last active
October 20, 2022 12:18
-
-
Save mgravell/575aada0e62dce9c004728796b559f36 to your computer and use it in GitHub Desktop.
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
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using System; | |
using System.Linq; | |
[MemoryDiagnoser] | |
public class ByteArrayChunker | |
{ | |
private readonly byte[] data; | |
public ByteArrayChunker() | |
{ | |
data = new byte[(16 * 1024) + 23]; | |
new Random(42).NextBytes(data); | |
} | |
[Params(-1, 0, 1, 1024, 16 * 1024)] | |
public int Offset { get; set; } | |
[Benchmark(Baseline = true)] | |
public byte[] Skip() => data.Skip(Offset).ToArray(); | |
[Benchmark] | |
public byte[] BlockCopy() | |
{ | |
if (Offset <= 0) return data; | |
var chunk = new byte[data.Length - Offset]; | |
Buffer.BlockCopy(data, Offset, chunk, 0, chunk.Length); | |
return chunk; | |
} | |
[Benchmark] | |
public ArraySegment<byte> Segment() | |
{ | |
if (Offset <= 0) return new ArraySegment<byte>(data); | |
return new ArraySegment<byte>(data, Offset, data.Length - Offset); | |
} | |
} | |
public static class Program | |
{ | |
public static void Main() => BenchmarkRunner.Run<ByteArrayChunker>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment