Skip to content

Commit 3bebe19

Browse files
author
Petr Sramek
committed
becnhmark updates
1 parent 150ca35 commit 3bebe19

File tree

6 files changed

+120
-94
lines changed

6 files changed

+120
-94
lines changed

benchmarks/PolylineAlgorithm.Benchmarks/PolylineBenchmark.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ namespace PolylineAlgorithm.Benchmarks;
1717
public class PolylineBenchmark {
1818
private static readonly Consumer consumer = new();
1919

20-
[Params(1, 10, 100, 250, 500, 1_000, 2_500, 5_000, 7_500, 10_000, 15_000, 20_000, 25_000, 50_000, 75_000, 100_000, 250_000, 500_000, 750_000, 1_000_000)]
21-
public int Length;
20+
[Params(1, 25, 50, 100, 250, 500, 1_000, 5_000, 10_000, 25_000, 50_000, 100_000, 500_000, 1_000_000)]
21+
public int Count;
2222

2323
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2424
/// <summary>
@@ -41,6 +41,13 @@ public class PolylineBenchmark {
4141
/// </summary>
4242
public string StringValue { get; private set; }
4343

44+
/// <summary>
45+
/// Gets the read-only memory representing the encoded polyline.
46+
/// </summary>
47+
public Polyline PolylineNotEqualValue { get; private set; }
48+
49+
public char[] CopyToDestination { get; private set; }
50+
4451
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
4552

4653

@@ -49,10 +56,13 @@ public class PolylineBenchmark {
4956
/// </summary>
5057
[GlobalSetup]
5158
public void SetupData() {
52-
PolylineValue = ValueProvider.GetPolyline(Length);
59+
PolylineValue = ValueProvider.GetPolyline(Count);
60+
PolylineNotEqualValue = ValueProvider.GetPolyline(Count + Random.Shared.Next(1, 101));
5361
StringValue = PolylineValue.ToString();
54-
CharArrayValue = StringValue.ToArray();
62+
CharArrayValue = [.. StringValue];
5563
MemoryValue = CharArrayValue.AsMemory();
64+
65+
CopyToDestination = new char[PolylineValue.Length];
5666
}
5767

5868
/// <summary>
@@ -103,31 +113,17 @@ public string Polyline_ToString() {
103113
return stringValue;
104114
}
105115

106-
///// <summary>
107-
///// Benchmarks the encoding of an enumeration of coordinates into a polyline.
108-
///// </summary>
109-
///// <returns>The encoded polyline.</returns>
110-
//[Benchmark]
111-
//public long Polyline_GetCoordinateCount() {
112-
// var coordinateCount = PolylineValue
113-
// .GetCoordinateCount();
114-
115-
// return coordinateCount;
116-
//}
117-
118116

119117
/// <summary>
120118
/// Benchmarks the encoding of an enumeration of coordinates into a polyline.
121119
/// </summary>
122120
/// <returns>The encoded polyline.</returns>
123121
[Benchmark]
124122
public void Polyline_CopyTo() {
125-
var destination = new char[PolylineValue.Length];
126-
127123
PolylineValue
128-
.CopyTo(destination);
124+
.CopyTo(CopyToDestination);
129125

130-
destination
126+
CopyToDestination
131127
.Consume(consumer);
132128
}
133129

@@ -136,10 +132,22 @@ public void Polyline_CopyTo() {
136132
/// </summary>
137133
/// <returns>The encoded polyline.</returns>
138134
[Benchmark]
139-
public bool Polyline_Equals() {
135+
public bool Polyline_Equals_SameValue() {
140136
var equals = PolylineValue
141137
.Equals(PolylineValue);
142138

143139
return equals;
144140
}
141+
142+
/// <summary>
143+
/// Benchmarks the encoding of an enumeration of coordinates into a polyline.
144+
/// </summary>
145+
/// <returns>The encoded polyline.</returns>
146+
[Benchmark]
147+
public bool Polyline_Equals_DifferentValue() {
148+
var equals = PolylineValue
149+
.Equals(PolylineNotEqualValue);
150+
151+
return equals;
152+
}
145153
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// Copyright © Pete Sramek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace PolylineAlgorithm.Benchmarks;
7+
8+
using BenchmarkDotNet.Attributes;
9+
using PolylineAlgorithm;
10+
using PolylineAlgorithm.Utility;
11+
12+
/// <summary>
13+
/// Benchmarks for the <see cref="PolylineValue"/> struct.
14+
/// </summary>
15+
[RankColumn]
16+
public class PolylineBuilderBenchmark {
17+
[Params(1, 10, 100, 500, 1_000)]
18+
public int Count;
19+
20+
[Params(1, 5, 10, 50, 100)]
21+
public int SegmentsCount;
22+
23+
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
24+
/// <summary>
25+
/// Gets the character array representing the encoded polyline.
26+
/// </summary>
27+
public char[] CharArrayValue { get; private set; }
28+
29+
/// <summary>
30+
/// Gets the read-only memory representing the encoded polyline.
31+
/// </summary>
32+
public ReadOnlyMemory<char> MemoryValue { get; private set; }
33+
34+
35+
/// <summary>
36+
/// Gets the string value representing the encoded polyline.
37+
/// </summary>
38+
public string StringValue { get; private set; }
39+
40+
/// <summary>
41+
/// Gets the read-only memory representing the encoded polyline.
42+
/// </summary>
43+
public Polyline PolylineNotEqualValue { get; private set; }
44+
45+
public char[] CopyToDestination { get; private set; }
46+
47+
internal Polyline.PolylineBuilder Builder { get; private set; }
48+
49+
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
50+
51+
52+
/// <summary>
53+
/// Sets up the data for the benchmarks.
54+
/// </summary>
55+
[GlobalSetup]
56+
public void SetupData() {
57+
Builder = new Polyline.PolylineBuilder();
58+
59+
var polyline = ValueProvider.GetPolyline(Count);
60+
StringValue = polyline.ToString();
61+
CharArrayValue = [.. StringValue];
62+
MemoryValue = CharArrayValue.AsMemory();
63+
}
64+
65+
/// <summary>
66+
/// Benchmarks the encoding of a list of coordinates into a polyline.
67+
/// </summary>
68+
/// <returns>The encoded polyline.</returns>
69+
[Benchmark]
70+
public Polyline PolylineBuilder_Append_Memory() {
71+
for(int i = 0; i < SegmentsCount; i++) {
72+
Builder
73+
.Append(MemoryValue);
74+
}
75+
76+
return Builder.Build();
77+
}
78+
}

benchmarks/PolylineAlgorithm.Benchmarks/PolylineDecoderBenchmark.cs

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,14 @@ namespace PolylineAlgorithm.Benchmarks;
1818
public class PolylineDecoderBenchmark {
1919
private readonly Consumer _consumer = new();
2020

21-
[Params(1, 10, 100, 250, 500, 1_000, 2_500, 5_000, 7_500, 10_000, 15_000, 20_000, 25_000, 50_000, 75_000, 100_000, 250_000, 500_000, 750_000, 1_000_000)]
22-
public int N;
21+
[Params(1, 25, 50, 100, 250, 500, 1_000, 5_000, 10_000, 25_000, 50_000, 100_000, 500_000, 1_000_000)]
22+
public int Count;
2323

2424
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2525
/// <summary>
2626
/// Gets the string value representing the encoded polyline.
2727
/// </summary>
28-
public string StringValue { get; private set; }
29-
30-
/// <summary>
31-
/// Gets the character array representing the encoded polyline.
32-
/// </summary>
33-
public char[] CharArray { get; private set; }
34-
35-
/// <summary>
36-
/// Gets the read-only memory representing the encoded polyline.
37-
/// </summary>
38-
public ReadOnlyMemory<char> Memory { get; private set; }
28+
public Polyline Polyline { get; private set; }
3929

4030
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
4131

@@ -49,44 +39,16 @@ public class PolylineDecoderBenchmark {
4939
/// </summary>
5040
[GlobalSetup]
5141
public void SetupData() {
52-
StringValue = ValueProvider.GetPolyline(N).ToString();
53-
CharArray = StringValue.ToArray();
54-
Memory = CharArray.AsMemory();
42+
Polyline = ValueProvider.GetPolyline(Count);
5543
}
5644

5745
/// <summary>
5846
/// Benchmarks the decoding of a polyline from a string.
5947
/// </summary>
6048
[Benchmark]
61-
public void PolylineDecoder_Decode_FromString() {
62-
Polyline polyline = Polyline.FromString(StringValue);
63-
64-
Decoder
65-
.Decode(StringValue)
66-
.Consume(_consumer);
67-
}
68-
69-
/// <summary>
70-
/// Benchmarks the decoding of a polyline from a character array.
71-
/// </summary>
72-
[Benchmark]
73-
public void PolylineDecoder_Decode_FromCharArray() {
74-
Polyline polyline = Polyline.FromCharArray(CharArray);
75-
76-
Decoder
77-
.Decode(StringValue)
78-
.Consume(_consumer);
79-
}
80-
81-
/// <summary>
82-
/// Benchmarks the decoding of a polyline from read-only memory.
83-
/// </summary>
84-
[Benchmark]
85-
public void PolylineDecoder_Decode_FromMemory() {
86-
Polyline polyline = Polyline.FromMemory(Memory);
87-
49+
public void PolylineDecoder_Decode() {
8850
Decoder
89-
.Decode(StringValue)
51+
.Decode(Polyline)
9052
.Consume(_consumer);
9153
}
9254
}

benchmarks/PolylineAlgorithm.Benchmarks/PolylineEncoderBenchmark.cs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ namespace PolylineAlgorithm.Benchmarks;
1616
/// </summary>
1717
[RankColumn]
1818
public class PolylineEncoderBenchmark {
19-
[Params(1, 10, 100, 250, 500, 1_000, 2_500, 5_000, 7_500, 10_000, 15_000, 20_000, 25_000, 50_000, 75_000, 100_000, 250_000, 500_000, 750_000, 1_000_000)]
20-
public int N;
19+
[Params(1, 25, 50, 100, 250, 500, 1_000, 5_000, 10_000, 25_000, 50_000, 100_000, 500_000, 1_000_000)]
20+
public int Count;
2121

2222
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2323
/// <summary>
2424
/// Gets the enumeration of coordinates to be encoded.
2525
/// </summary>
26-
public static IEnumerable<Coordinate> Enumeration { get; private set; }
26+
public IEnumerable<Coordinate> Enumeration { get; private set; }
2727

2828
/// <summary>
2929
/// Gets the list of coordinates to be encoded.
3030
/// </summary>
31-
public static List<Coordinate> List { get; private set; }
32-
33-
public static IAsyncEnumerable<Coordinate> AsyncEnumeration { get; private set; }
31+
public List<Coordinate> List { get; private set; }
3432
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
3533

3634
/// <summary>
@@ -48,15 +46,8 @@ public class PolylineEncoderBenchmark {
4846
/// </summary>
4947
[GlobalSetup]
5048
public void SetupData() {
51-
Enumeration = ValueProvider.GetCoordinates(N);
49+
Enumeration = ValueProvider.GetCoordinates(Count);
5250
List = [.. Enumeration];
53-
AsyncEnumeration = GetAsyncEnumeration(Enumeration!);
54-
}
55-
56-
private async IAsyncEnumerable<Coordinate> GetAsyncEnumeration(IEnumerable<Coordinate> enumerable) {
57-
foreach (var item in enumerable) {
58-
yield return await new ValueTask<Coordinate>(item);
59-
}
6051
}
6152

6253
/// <summary>
@@ -82,17 +73,4 @@ public Polyline PolylineEncoder_Encode_Enumerator() {
8273

8374
return polyline;
8475
}
85-
86-
/// <summary>
87-
/// Benchmarks the encoding of an enumeration of coordinates into a polyline.
88-
/// </summary>
89-
/// <returns>The encoded polyline.</returns>
90-
//[Benchmark]
91-
//public async Task<Polyline> PolylineEncoder_EncodeAsync_String() {
92-
// var polyline = await AsyncEncoder
93-
// .EncodeAsync(AsyncEnumeration!)
94-
// .ConfigureAwait(false);
95-
96-
// return polyline;
97-
//}
9876
}

benchmarks/PolylineAlgorithm.Comparison.Benchmarks/PolylineDecoderBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public class PolylineDecoderBenchmark {
5353
/// </summary>
5454
[GlobalSetup]
5555
public void SetupData() {
56-
StringValue = ValueProvider.GetPolyline(Count).ToString();
5756
PolylineValue = ValueProvider.GetPolyline(Count);
57+
StringValue = PolylineValue.ToString();
5858
}
5959

6060
/// <summary>

src/PolylineAlgorithm/PolylineAlgorithm.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<_Parameter1>PolylineAlgorithm.Tests</_Parameter1>
7171
</AssemblyAttribute>
7272
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
73-
<_Parameter1>PolylineAlgorithm.IO.Pipelines</_Parameter1>
73+
<_Parameter1>PolylineAlgorithm.Benchmarks</_Parameter1>
7474
</AssemblyAttribute>
7575
</ItemGroup>
7676

0 commit comments

Comments
 (0)