Skip to content

Commit 36c64a0

Browse files
authored
Added regex benchmarks (#28)
***NO_CI***
1 parent 1499834 commit 36c64a0

File tree

9 files changed

+396
-0
lines changed

9 files changed

+396
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
namespace nanoFramework.System.Text.Benchmark
8+
{
9+
public interface IAssemblyHandler
10+
{
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using nanoFramework.Benchmark;
8+
using nanoFramework.System.Text.Benchmark;
9+
using System.Threading;
10+
11+
namespace nanoFramework.System.Text.RegularExpression.Benchmark
12+
{
13+
public class Program
14+
{
15+
public static void Main()
16+
{
17+
BenchmarkRunner.Run(typeof(IAssemblyHandler).Assembly);
18+
Thread.Sleep(Timeout.Infinite);
19+
}
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("CSharp.BlankApplication")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("CSharp.BlankApplication")]
13+
[assembly: AssemblyCopyright("Copyright © 2022")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// Version information for an assembly consists of the following four values:
23+
//
24+
// Major Version
25+
// Minor Version
26+
// Build Number
27+
// Revision
28+
//
29+
// You can specify all the values or you can default the Build and Revision Numbers
30+
// by using the '*' as shown below:
31+
// [assembly: AssemblyVersion("1.0.*")]
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using nanoFramework.Benchmark;
8+
using nanoFramework.Benchmark.Attributes;
9+
using System.Diagnostics;
10+
using System.Text;
11+
using System.Text.RegularExpressions;
12+
13+
namespace nanoFramework.System.Text.RegularExpression.Benchmark
14+
{
15+
[DebugLogger]
16+
[ConsoleParser]
17+
[IterationCount(100)]
18+
public class RegexMatchBenchmark : RegexBenchmarkBase
19+
{
20+
public override object MethodToBenchmark(string input, string pattern, RegexOptions options = default)
21+
{
22+
if (options != default(RegexOptions))
23+
{
24+
return Regex.Match(input, pattern);
25+
}
26+
else
27+
{
28+
return Regex.Match(input, pattern, options);
29+
}
30+
}
31+
}
32+
33+
[DebugLogger]
34+
[ConsoleParser]
35+
[IterationCount(100)]
36+
public class RegexMatchesBenchmark : RegexBenchmarkBase
37+
{
38+
public override object MethodToBenchmark(string input, string pattern, RegexOptions options = default)
39+
{
40+
if (options != default(RegexOptions))
41+
{
42+
return Regex.Matches(input, pattern);
43+
}
44+
else
45+
{
46+
return Regex.Matches(input, pattern, options);
47+
}
48+
}
49+
}
50+
51+
52+
public abstract class RegexBenchmarkBase
53+
{
54+
private readonly string _input;
55+
private readonly string _input1000x;
56+
57+
public RegexBenchmarkBase()
58+
{
59+
Debug.WriteLine($"\n\nStarting {this.GetType().Name}...");
60+
61+
_input = "This is a test. ";
62+
var builder = new StringBuilder();
63+
for (var j = 0; j < 1000; j++)
64+
{
65+
builder.Append(_input);
66+
}
67+
_input1000x = builder.ToString();
68+
}
69+
70+
public abstract object MethodToBenchmark(string input, string pattern, RegexOptions options = default);
71+
72+
[Benchmark]
73+
public void Regex_Match()
74+
{
75+
_ = MethodToBenchmark(_input, "test");
76+
}
77+
78+
[Benchmark]
79+
public void Regex_Match_LargerInput()
80+
{
81+
_ = MethodToBenchmark(_input1000x, "test");
82+
}
83+
84+
[Benchmark]
85+
public void Regex_Match_LongerPattern()
86+
{
87+
_ = MethodToBenchmark(_input, "This is a test");
88+
}
89+
90+
[Benchmark]
91+
public void Regex_Match_ShorterPattern()
92+
{
93+
_ = MethodToBenchmark(_input, "s");
94+
}
95+
96+
[Benchmark]
97+
public void Regex_Match_100xInput_WhitespaceInPattern()
98+
{
99+
_ = MethodToBenchmark(_input, @"\sa\s");
100+
}
101+
102+
[Benchmark]
103+
public void Regex_Match_100xInput_WordInPattern()
104+
{
105+
_ = MethodToBenchmark(_input, @"\w");
106+
}
107+
108+
[Benchmark]
109+
public void Regex_Match_100xInput_WildcardInPattern()
110+
{
111+
_ = MethodToBenchmark(_input, @".s");
112+
}
113+
114+
[Benchmark]
115+
public void Regex_Match_IgnoreCase()
116+
{
117+
_ = MethodToBenchmark(_input, "Test", RegexOptions.IgnoreCase);
118+
}
119+
120+
[Benchmark]
121+
public void Regex_Match_Multiline()
122+
{
123+
_ = MethodToBenchmark(_input, "Test", RegexOptions.Multiline);
124+
}
125+
126+
[Benchmark]
127+
public void Regex_Match_IgnorePatternWhitespace()
128+
{
129+
_ = MethodToBenchmark(_input, "Test", RegexOptions.IgnorePatternWhitespace);
130+
}
131+
}
132+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using nanoFramework.Benchmark;
8+
using nanoFramework.Benchmark.Attributes;
9+
using System.Diagnostics;
10+
using System.Text;
11+
using System.Text.RegularExpressions;
12+
13+
namespace nanoFramework.System.Text.RegularExpression.Benchmark
14+
{
15+
[DebugLogger]
16+
[ConsoleParser]
17+
[IterationCount(100)]
18+
public class RegexReplaceBenchmark
19+
{
20+
private readonly string _input;
21+
private readonly string _input10x;
22+
private readonly Regex _regex;
23+
24+
public RegexReplaceBenchmark()
25+
{
26+
Debug.WriteLine($"\n\nStarting {this.GetType().Name}...");
27+
28+
_input = "This is a test. ";
29+
var builder = new StringBuilder();
30+
for (var j = 0; j < 10; j++)
31+
{
32+
builder.Append(_input);
33+
}
34+
_input10x = builder.ToString();
35+
36+
_regex = new Regex("test");
37+
}
38+
39+
40+
[Benchmark]
41+
public void Regex_Replace()
42+
{
43+
_ = _regex.Replace(_input, "replacement");
44+
}
45+
46+
[Benchmark]
47+
public void Regex_Replace_LargerInput()
48+
{
49+
_ = _regex.Replace(_input10x, "replacement");
50+
}
51+
52+
[Benchmark]
53+
public void Regex_Replace_LargerInput_MaxOccurrences1()
54+
{
55+
_ = _regex.Replace(_input10x, "replacement", 1, 0);
56+
}
57+
58+
[Benchmark]
59+
public void Regex_Replace_LargerInput_MaxOccurrences5()
60+
{
61+
_ = _regex.Replace(_input10x, "replacement", 5, 0);
62+
}
63+
}
64+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>0aba6a05-c71e-470a-9928-16fa4fd097ba</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>nanoFramework.System.Text.RegularExpression.Benchmark</RootNamespace>
16+
<AssemblyName>nanoFramework.System.Text.RegularExpression.Benchmark</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
19+
<RestoreLockedMode Condition="'$(TF_BUILD)' == 'True' or '$(ContinuousIntegrationBuild)' == 'True'">true</RestoreLockedMode>
20+
</PropertyGroup>
21+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
22+
<ItemGroup>
23+
<Compile Include="IAssemblyHandler.cs" />
24+
<Compile Include="Program.cs" />
25+
<Compile Include="Properties\AssemblyInfo.cs" />
26+
<Compile Include="RegexReplaceBenchmark.cs" />
27+
<Compile Include="RegexMatchBenchmark.cs" />
28+
</ItemGroup>
29+
<ItemGroup>
30+
<ProjectReference Include="..\nanoFramework.System.Text.RegularExpressions\nanoFramework.System.Text.RegularExpressions.nfproj">
31+
<Aliases>
32+
</Aliases>
33+
</ProjectReference>
34+
</ItemGroup>
35+
<ItemGroup>
36+
<Reference Include="mscorlib, Version=1.12.0.4, Culture=neutral, PublicKeyToken=c07d481e9758c731">
37+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
38+
</Reference>
39+
<Reference Include="nanoFramework.Benchmark">
40+
<HintPath>..\packages\nanoFramework.Benchmark.1.0.26\lib\nanoFramework.Benchmark.dll</HintPath>
41+
</Reference>
42+
<Reference Include="nanoFramework.Logging">
43+
<HintPath>..\packages\nanoFramework.Logging.1.1.25\lib\nanoFramework.Logging.dll</HintPath>
44+
</Reference>
45+
<Reference Include="nanoFramework.Runtime.Native">
46+
<HintPath>..\packages\nanoFramework.Runtime.Native.1.5.4\lib\nanoFramework.Runtime.Native.dll</HintPath>
47+
</Reference>
48+
<Reference Include="nanoFramework.System.Collections">
49+
<HintPath>..\packages\nanoFramework.System.Collections.1.4.0\lib\nanoFramework.System.Collections.dll</HintPath>
50+
</Reference>
51+
<Reference Include="nanoFramework.System.Text">
52+
<HintPath>..\packages\nanoFramework.System.Text.1.2.7\lib\nanoFramework.System.Text.dll</HintPath>
53+
</Reference>
54+
<Reference Include="System.Diagnostics.Stopwatch">
55+
<HintPath>..\packages\nanoFramework.System.Diagnostics.Stopwatch.1.2.82\lib\System.Diagnostics.Stopwatch.dll</HintPath>
56+
</Reference>
57+
</ItemGroup>
58+
<ItemGroup>
59+
<None Include="packages.config" />
60+
</ItemGroup>
61+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
62+
<ProjectExtensions>
63+
<ProjectCapabilities>
64+
<ProjectConfigurationsDeclaredAsItems />
65+
</ProjectCapabilities>
66+
</ProjectExtensions>
67+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.Benchmark" version="1.0.26" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnano1.0" />
5+
<package id="nanoFramework.Logging" version="1.1.25" targetFramework="netnano1.0" />
6+
<package id="nanoFramework.Runtime.Native" version="1.5.4" targetFramework="netnano1.0" />
7+
<package id="nanoFramework.System.Collections" version="1.4.0" targetFramework="netnano1.0" />
8+
<package id="nanoFramework.System.Diagnostics.Stopwatch" version="1.2.82" targetFramework="netnano1.0" />
9+
<package id="nanoFramework.System.Text" version="1.2.7" targetFramework="netnano1.0" />
10+
</packages>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"version": 1,
3+
"dependencies": {
4+
".NETnanoFramework,Version=v1.0": {
5+
"nanoFramework.Benchmark": {
6+
"type": "Direct",
7+
"requested": "[1.0.26, 1.0.26]",
8+
"resolved": "1.0.26",
9+
"contentHash": "Fs6UFjU4F8trdT2VMFzg7Fmy8lC1/0168v4v5CUE0MuM5wsBNAmCsvEXLPxn7prNqz+6LD9CYrjrJzIxhmmJJA=="
10+
},
11+
"nanoFramework.CoreLibrary": {
12+
"type": "Direct",
13+
"requested": "[1.12.0, 1.12.0]",
14+
"resolved": "1.12.0",
15+
"contentHash": "qQrFNXmJiStMC4VXk5cVMOJp23/qlT9FW5i9i+igwQVwraQTtvpkam8yK1hj992jqrbjoCIFZP4Hw9E8H0pB7w=="
16+
},
17+
"nanoFramework.Logging": {
18+
"type": "Direct",
19+
"requested": "[1.1.25, 1.1.25]",
20+
"resolved": "1.1.25",
21+
"contentHash": "mq1rzZmHmaf827BZlaaqamUq58YCF3l7Y6oaKnd+Gt7B2yqj9MjgABu2MqjT82aMJBNuAl2LV55mEqMJZQaPjw=="
22+
},
23+
"nanoFramework.Runtime.Native": {
24+
"type": "Direct",
25+
"requested": "[1.5.4, 1.5.4]",
26+
"resolved": "1.5.4",
27+
"contentHash": "qfaOY1O5TOEw//MO4AOWRq5CdZXTfd3KnsGTNU2yw+IEYKiQLPkJhtF3ufF/S04xfXL556S2kOFG3/RZrwQ4Qw=="
28+
},
29+
"nanoFramework.System.Collections": {
30+
"type": "Direct",
31+
"requested": "[1.4.0, 1.4.0]",
32+
"resolved": "1.4.0",
33+
"contentHash": "/yFwxtCFzi+24NuyxcwlH1YyBGOxRX4oHGLwVmFbgbvOyx3ny/Mwyk2YjHTzmTSgUg9C2XxPF+EkXWwCOAkytw=="
34+
},
35+
"nanoFramework.System.Diagnostics.Stopwatch": {
36+
"type": "Direct",
37+
"requested": "[1.2.82, 1.2.82]",
38+
"resolved": "1.2.82",
39+
"contentHash": "Ehqs+IV7EVqXREWCq2csMzDn8/QXCAwTzMs6HgMv+1EZywNA/hwtLgNbbeFSxb4DtMfz7uKJZ5I7UM/rbTHkRg=="
40+
},
41+
"nanoFramework.System.Text": {
42+
"type": "Direct",
43+
"requested": "[1.2.7, 1.2.7]",
44+
"resolved": "1.2.7",
45+
"contentHash": "ax4CrqYHNXoEBjaIW7dIv1CM4PxWcCc6lN2xSzGLZf7F/JZ0O7t0el86PPN2P/DfOyyBKSDAmrZjEx4q8mvgSw=="
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)