-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
141 lines (126 loc) · 4.71 KB
/
Program.cs
File metadata and controls
141 lines (126 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Configs;
namespace TestApp
{
/// <summary>
/// ベンチマークメソッド群を提供するクラス。
/// </summary>
[GroupBenchmarksBy(
BenchmarkLogicalGroupRule.ByCategory,
BenchmarkLogicalGroupRule.ByParams)]
public class BenchmarkMethods
{
/// <summary>
/// '\n' による分割対象文字列。
/// </summary>
private string textWithNewLine = @"";
/// <summary>
/// 各種改行コード("\r\n", "\n", "\r")による分割対象文字列。
/// </summary>
private string textWithLineBreaks = @"";
/// <summary>
/// 分割対象文字列の行数。 BenchmarkDotNet によって初期化される。
/// </summary>
[Params(1, 10, 100, 1000, 10000)]
public int Line { get; set; }
/// <summary>
/// ベンチマーク前処理を行う。
/// </summary>
[GlobalSetup]
public void Setup()
{
this.textWithNewLine = MakeString(this.Line, "\n");
this.textWithLineBreaks = MakeString(this.Line, LineBreaks);
}
/// <summary>
/// string.Split('\n') を行う。
/// </summary>
/// <returns>分割文字列配列。</returns>
[Benchmark(Baseline = true), BenchmarkCategory(@"NewLine")]
public string[] String_SplitNewLine() => this.textWithNewLine.Split('\n');
/// <summary>
/// ReadOnlySpan{char}.SplitToRanges('\n') を用いた文字列分割を行う。
/// </summary>
/// <returns>分割文字列配列。</returns>
[Benchmark, BenchmarkCategory(@"NewLine")]
public string[] Span_SplitNewLine()
{
var span = this.textWithNewLine.AsSpan();
var ranges = span.SplitToRanges('\n');
var lines = new string[ranges.Count];
for (int li = 0; li < lines.Length; ++li)
{
lines[li] = span[ranges[li]].ToString();
}
return lines;
}
/// <summary>
/// string.Split(LineBreaks) を行う。
/// </summary>
/// <returns>分割文字列配列。</returns>
[Benchmark(Baseline = true), BenchmarkCategory(@"LineBreaks")]
public string[] String_SplitLineBreaks() =>
this.textWithLineBreaks.Split(LineBreaks, StringSplitOptions.None);
/// <summary>
/// ReadOnlySpan{char}.SplitToRanges(LineBreaks) を用いた文字列分割を行う。
/// </summary>
/// <returns>分割文字列配列。</returns>
[Benchmark, BenchmarkCategory(@"LineBreaks")]
public string[] Span_SplitLineBreaks()
{
var span = this.textWithLineBreaks.AsSpan();
var ranges = span.SplitToRanges(LineBreaks);
var lines = new string[ranges.Count];
for (int li = 0; li < lines.Length; ++li)
{
lines[li] = span[ranges[li]].ToString();
}
return lines;
}
/// <summary>
/// 改行文字列配列。
/// </summary>
private static readonly string[] LineBreaks = { "\r\n", "\n", "\r" };
/// <summary>
/// 指定した行数で行頭と行末に空白文字を持つ文字列を作成する。
/// </summary>
/// <param name="lineCount">行数。</param>
/// <param name="splitters">行分割文字列配列。指定無しならば "\n" が使われる。</param>
/// <returns>作成した文字列。</returns>
private static string MakeString(int lineCount, params string[] splitters)
{
var sp = (splitters.Length == 0) ? new[] { "\n" } : splitters;
var builder = new StringBuilder();
for (int li = 0; li < lineCount; ++li)
{
if (li != 0)
{
builder.Append(sp[li % sp.Length]);
}
builder.Append(
new string(' ', li % 10) +
new string('a', (li + 100) % 1000 + 1) +
new string('\t', (li + 5) % 26));
}
return builder.ToString();
}
}
/// <summary>
/// ReadOnlySpan{T}.SplitToRanges ベンチマークアプリクラス。
/// </summary>
internal static class Program
{
/// <summary>
/// メインエントリポイント。
/// </summary>
private static int Main()
{
// ベンチマーク実施
BenchmarkRunner.Run<BenchmarkMethods>();
return 0;
}
}
}