|
1 | | -using System; |
2 | 1 | using System.Text; |
3 | | -using System.Diagnostics; |
| 2 | +using BenchmarkDotNet.Attributes; |
4 | 3 |
|
5 | | -namespace ConvertInt2String |
6 | | -{ |
7 | | - public static class Benchmark |
8 | | - { |
9 | | - const int formatOffsetLeft = 23; |
10 | | - const int formatOffsetRight = 10; |
11 | | - |
12 | | - static string BenchmarkToString(int num, int reps) |
13 | | - { |
14 | | - var watch = new Stopwatch(); |
15 | | - watch.Start(); |
16 | | - for (int i = 0; i < reps; i++) |
17 | | - { |
18 | | - var tmpResult = num.ToString(); |
19 | | - } |
20 | | - watch.Stop(); |
| 4 | +namespace ConvertInt2String; |
21 | 5 |
|
22 | | - return $"|{"'ToString'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|"; |
23 | | - } |
24 | | - static string BenchmarkConvertToString(int num, int reps) |
25 | | - { |
26 | | - var watch = new Stopwatch(); |
27 | | - watch.Start(); |
28 | | - for (int i = 0; i < reps; i++) |
29 | | - { |
30 | | - var tmpResult = num.ToString(); |
31 | | - } |
32 | | - watch.Stop(); |
33 | | - |
34 | | - return $"|{"'ConvertToString'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|"; |
35 | | - } |
36 | | - static string BenchmarkStringFormat(int num, int reps) |
37 | | - { |
38 | | - var watch = new Stopwatch(); |
39 | | - watch.Start(); |
40 | | - for (int i = 0; i < reps; i++) |
41 | | - { |
42 | | - var tmpResult = string.Format("{0}", num); |
43 | | - } |
44 | | - watch.Stop(); |
| 6 | +[MemoryDiagnoser] |
| 7 | +public class IntToStringBenchmarks |
| 8 | +{ |
| 9 | + [Params(3, 1_234_567)] |
| 10 | + public int Number { get; set; } |
45 | 11 |
|
46 | | - return $"|{"'String.Format'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|"; |
47 | | - } |
48 | | - static string BenchmarkStringInterpolation(int num, int reps) |
49 | | - { |
50 | | - var watch = new Stopwatch(); |
51 | | - watch.Start(); |
52 | | - for (int i = 0; i < reps; i++) |
53 | | - { |
54 | | - var tmpResult = $"{num}"; |
55 | | - } |
56 | | - watch.Stop(); |
| 12 | + [Benchmark(Baseline = true)] |
| 13 | + public string IntToString() => Number.ToString(); |
57 | 14 |
|
58 | | - return $"|{"'String Interpolation'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|"; |
59 | | - } |
60 | | - static string BenchmarkStringConcat(int num, int reps) |
61 | | - { |
62 | | - var watch = new Stopwatch(); |
63 | | - watch.Start(); |
64 | | - for (int i = 0; i < reps; i++) |
65 | | - { |
66 | | - var tmpResult = string.Concat(string.Empty, num); |
67 | | - } |
68 | | - watch.Stop(); |
| 15 | + [Benchmark] |
| 16 | + public string ConvertToString() => Convert.ToString(Number); |
69 | 17 |
|
70 | | - return $"|{"'String.Concat'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|"; |
71 | | - } |
72 | | - static string BenchmarkStringJoin(int num, int reps) |
73 | | - { |
74 | | - var watch = new Stopwatch(); |
75 | | - watch.Start(); |
76 | | - for (int i = 0; i < reps; i++) |
77 | | - { |
78 | | - var tmpResult = string.Join(string.Empty, num); |
79 | | - } |
80 | | - watch.Stop(); |
| 18 | + [Benchmark] |
| 19 | + public string StringFormat() => string.Format("{0}", Number); |
81 | 20 |
|
82 | | - return $"|{"'String.Join'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|"; |
83 | | - } |
84 | | - static string BenchmarkPlusSign(int num, int reps) |
85 | | - { |
86 | | - var watch = new Stopwatch(); |
87 | | - watch.Start(); |
88 | | - for (int i = 0; i < reps; i++) |
89 | | - { |
90 | | - var tmpResult = string.Empty + num; |
91 | | - } |
92 | | - watch.Stop(); |
| 21 | + [Benchmark] |
| 22 | + public string StringInterpolation() => $"{Number}"; |
93 | 23 |
|
94 | | - return $"|{"'Plus sign'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|"; |
95 | | - } |
96 | | - static string BenchmarkStringBuilderAppend(int num, int reps) |
97 | | - { |
98 | | - var watch = new Stopwatch(); |
99 | | - watch.Start(); |
100 | | - for (int i = 0; i < reps; i++) |
101 | | - { |
102 | | - var tmpResult = new StringBuilder().Append(num).ToString(); |
103 | | - } |
104 | | - watch.Stop(); |
| 24 | + [Benchmark] |
| 25 | + public string StringConcat() => string.Concat(string.Empty, Number); |
105 | 26 |
|
106 | | - return $"|{"'Stringbuilder Append'",formatOffsetLeft} | {watch.ElapsedMilliseconds,formatOffsetRight:N0}|"; |
107 | | - } |
| 27 | + [Benchmark] |
| 28 | + public string StringJoin() => string.Join(string.Empty, Number); |
108 | 29 |
|
109 | | - public static string Run(int number, int repetitions) |
110 | | - { |
111 | | - var benchmarkResultsBuilder = new StringBuilder($"\r\nBenchmarks with {repetitions:N0} repetitions: \r\n") |
112 | | - .AppendLine($"|{"Method",formatOffsetLeft} | {"Speed (ms)",formatOffsetRight}|") |
113 | | - .AppendLine($"|{"-----------------------",formatOffsetLeft}-|-{"----------",formatOffsetRight}|") |
114 | | - .AppendLine(BenchmarkToString(number, repetitions)) |
115 | | - .AppendLine(BenchmarkConvertToString(number, repetitions)) |
116 | | - .AppendLine(BenchmarkStringFormat(number, repetitions)) |
117 | | - .AppendLine(BenchmarkStringInterpolation(number, repetitions)) |
118 | | - .AppendLine(BenchmarkStringConcat(number, repetitions)) |
119 | | - .AppendLine(BenchmarkStringJoin(number, repetitions)) |
120 | | - .AppendLine(BenchmarkPlusSign(number, repetitions)) |
121 | | - .AppendLine(BenchmarkStringBuilderAppend(number, repetitions)); |
| 30 | + [Benchmark] |
| 31 | + public string PlusSign() => string.Empty + Number; |
122 | 32 |
|
123 | | - return benchmarkResultsBuilder.ToString(); |
124 | | - } |
125 | | - } |
| 33 | + [Benchmark] |
| 34 | + public string StringBuilderAppend() => new StringBuilder().Append(Number).ToString(); |
126 | 35 | } |
0 commit comments