Hello Jialiang Ge [MSFT],
Hello Flomo
The parenthesis is not a must. I added it so as to make the logic
clearer.
You may remove the parenthesis:
Regex regex = new Regex("^\"|\"$");
Please feel free to let me know if you have any other concern.
Usually when you have two regexes that you combine, but have nothign in common (e.g. they do not
logically follow eachother) it's faster to run them seperately. Though these regexes are so short
this advantage might nbe lost.
It might be even faster to just load the string in a stringbuilder and use Remove on the first
and the last character if needed. Even a simple substring might be faster...
I tested all options with the code below and the end result was that with reasonably short
strings the Stringbuilder won, with larger strings the substring function won. followed after a
very large gat (almost 4x slower) by the regexes.
results in milliseconds for 10000 passes.
substring : 123
stringbuilder : 135
regexboth : 308
regextwopart : 443
I used compiled regexes and made sure the compile time was outside the stopwatch.
I've attached the code below, including the substring and stringbuilder funtions I used
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Collections;
namespace ConsoleApplication1
{
public delegate string TestStringDelegate(string input);
class Program
{
static Dictionary<string, long> results = new Dictionary<string, long>();
static Program()
{
replaceBoth.Match("");
replaceLeft.Match("");
replaceRight.Match("");
}
static void Main(string[] args)
{
string[] inputs = new string[]{
@"""........""",
@"""............................................................................""",
@"""..............................................................................................
..................................................................................................
..................................................................................................
...................................................................................""",
@"""..............................................................................................
..................................................................................................
..................................................................................................
...................................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
...............................................................................
..................................................................................................
..................................................................................................
..................................................................................................
............................................................................... """
}; foreach (string input in inputs) {
Test("substring", input, new TestStringDelegate(TestSubstring));
Test("stringbuilder", input, new TestStringDelegate(TestStringBuilder));
Test("regexboth", input, new TestStringDelegate(TestRegexBoth));
Test("regextwopart", input, new TestStringDelegate(TestRegexTwoPart)); }
WriteResults();
Console.ReadLine();
}
static void WriteResults()
{
foreach (string key in results.Keys)
{
Console.WriteLine("{0, -20}: {1}", key, results[key]);
}
}
static void Test(string name, string input, TestStringDelegate function )
{
string output = "";
Stopwatch sw = new Stopwatch();
Console.WriteLine("Start test " + name);
sw.Start();
for (int i = 0; i < 10000; i++)
{
output = function(input);
}
sw.Stop();
if (results.ContainsKey(name))
{
long millis = results[name];
results[name] = millis + sw.ElapsedMilliseconds;
}
else
{
results.Add(name, sw.ElapsedMilliseconds);
}
Console.WriteLine(input);
Console.WriteLine(output);
Console.WriteLine(sw.ElapsedMilliseconds);
Console.WriteLine("End test " + name);
}
static string TestStringBuilder(string input)
{
StringBuilder sb = new StringBuilder(input);
if (sb.Length > 0 && sb[0] == '\"')
{
sb.Remove(0, 1);
}
if (sb.Length > 1 && sb[sb.Length - 1] == '\"')
{
sb.Remove(sb.Length - 1, 1);
}
string output = sb.ToString();
return output;
}
static string TestSubstring(string input)
{
int start = 0;
int end = input.Length;
if (input.StartsWith("\""))
{
start++;
}
if (input.EndsWith("\"") && input.Length > 1)
{
end--;
}
string output = input.Substring(start, end - start);
return output;
}
static string TestRegexBoth(string input)
{
return replaceBoth.Replace(input, "");
}
static string TestRegexTwoPart(string input)
{
return replaceRight.Replace(replaceLeft.Replace(input, ""), "");
}
private static Regex replaceBoth = new Regex(@"^""|""$", RegexOptions.Compiled);
private static Regex replaceLeft = new Regex(@"^""", RegexOptions.Compiled);
private static Regex replaceRight = new Regex(@"""$", RegexOptions.Compiled);
}
}