Skip to content

Commit 11760ec

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 55dd1e3 + 48a6d75 commit 11760ec

File tree

12 files changed

+650
-37
lines changed

12 files changed

+650
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async void Example(int x, int y, int z)
9595
// create Utf8 StringBuilder that build Utf8 directly to avoid encoding
9696
using var sb2 = ZString.CreateUtf8StringBuilder();
9797

98-
sb2.Concat("foo:", x, ", bar:", y);
98+
sb2.AppendFormat("foo:{0} bar:{1}", x, y);
9999

100100
// directly write to steam or dest to avoid allocation
101101
await sb2.WriteToAsync(stream);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Cysharp.Text;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace PerfBenchmark.Benchmarks
8+
{
9+
[Config(typeof(BenchmarkConfig))]
10+
public class AppendPerformance
11+
{
12+
List<string> strings;
13+
const int COUNT = 1000;
14+
15+
public AppendPerformance()
16+
{
17+
strings = new List<string>();
18+
for (int i = 0; i < 100; i++)
19+
{
20+
strings.Add("123456789");
21+
}
22+
}
23+
24+
[Benchmark]
25+
public void ZStringUtf16()
26+
{
27+
for (int i = 0; i < COUNT; i++)
28+
{
29+
using (var sb = ZString.CreateStringBuilder())
30+
{
31+
for (int j = 0; j < strings.Count; j++)
32+
{
33+
sb.Append(strings[j]);
34+
}
35+
36+
_ = sb.ToString();
37+
}
38+
}
39+
}
40+
41+
//[Benchmark]
42+
//public void ZStringUtf16SpanBased()
43+
//{
44+
// for (int i = 0; i < COUNT; i++)
45+
// {
46+
// using (var sb = ZString.CreateStringBuilder())
47+
// {
48+
// for (int j = 0; j < strings.Count; j++)
49+
// {
50+
// sb.AppendSlow(strings[j]);
51+
// }
52+
53+
// _ = sb.ToString();
54+
// }
55+
// }
56+
//}
57+
58+
[Benchmark(Baseline = true)]
59+
public void StringBuilder()
60+
{
61+
for (int i = 0; i < COUNT; i++)
62+
{
63+
System.Text.StringBuilder sb = new System.Text.StringBuilder();
64+
for (int j = 0; j < strings.Count; j++)
65+
{
66+
sb.Append(strings[j]);
67+
}
68+
69+
_ = sb.ToString();
70+
}
71+
}
72+
}
73+
74+
75+
[Config(typeof(BenchmarkConfig))]
76+
public class Utf8AppendPerformance
77+
{
78+
List<string> strings;
79+
const int COUNT = 1000;
80+
81+
public Utf8AppendPerformance()
82+
{
83+
strings = new List<string>();
84+
for (int i = 0; i < 100; i++)
85+
{
86+
strings.Add("123456789");
87+
}
88+
}
89+
90+
[Benchmark]
91+
public void ZStringUtf8()
92+
{
93+
for (int i = 0; i < COUNT; i++)
94+
{
95+
using (var sb = ZString.CreateUtf8StringBuilder())
96+
{
97+
for (int j = 0; j < strings.Count; j++)
98+
{
99+
sb.Append(strings[j]);
100+
}
101+
102+
_ = sb.AsSpan().ToArray();
103+
}
104+
}
105+
}
106+
107+
[Benchmark(Baseline = true)]
108+
public void StringBuilder()
109+
{
110+
for (int i = 0; i < COUNT; i++)
111+
{
112+
System.Text.StringBuilder sb = new System.Text.StringBuilder();
113+
for (int j = 0; j < strings.Count; j++)
114+
{
115+
sb.Append(strings[j]);
116+
}
117+
118+
_ = sb.ToString();
119+
}
120+
}
121+
}
122+
}

sandbox/PerfBenchmark/Benchmarks/StringBuilderAppendJoin.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ public StringBuilderAppendJoin()
1717
fValues = new[] { 0f, float.MaxValue, float.MinValue };
1818
mValues = new[] { 0m, decimal.MaxValue, decimal.MinValue };
1919

20+
#if NETCOREAPP || NETSTANDARD2_1
2021
if (StringBuilder() != ZStringBuilder())
2122
throw new Exception();
23+
#endif
2224
}
2325

2426

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
6-
</PropertyGroup>
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net5.0</TargetFrameworks>
6+
<LangVersion>8.0</LangVersion>
7+
</PropertyGroup>
78

8-
<ItemGroup>
9-
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
10-
<PackageReference Include="StringFormatter" Version="1.0.0.13" />
11-
</ItemGroup>
9+
<ItemGroup>
10+
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
11+
<PackageReference Include="StringFormatter" Version="1.0.0.13" />
12+
</ItemGroup>
1213

13-
<ItemGroup>
14-
<ProjectReference Include="..\..\src\ZString\ZString.csproj" />
15-
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\src\ZString\ZString.csproj" />
16+
</ItemGroup>
1617

1718
</Project>

sandbox/PerfBenchmark/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BenchmarkDotNet.Attributes;
22
using BenchmarkDotNet.Configs;
33
using BenchmarkDotNet.Diagnosers;
4+
using BenchmarkDotNet.Environments;
45
using BenchmarkDotNet.Exporters;
56
using BenchmarkDotNet.Jobs;
67
using BenchmarkDotNet.Running;
@@ -17,7 +18,10 @@ internal class BenchmarkConfig : ManualConfig
1718
public BenchmarkConfig()
1819
{
1920
AddDiagnoser(MemoryDiagnoser.Default);
20-
AddJob(Job.ShortRun.WithWarmupCount(1).WithIterationCount(1));
21+
AddJob(Job.ShortRun.WithWarmupCount(1).WithIterationCount(1).WithRuntime(CoreRuntime.Core50));
22+
23+
// Add Targetframeworks net47 to csproj(removed for CI)
24+
//AddJob(Job.ShortRun.WithWarmupCount(1).WithIterationCount(1).WithRuntime(ClrRuntime.Net47));
2125
}
2226
}
2327

0 commit comments

Comments
 (0)