鉴于 MSDN 的以下内容:
正则表达式对象可以在任何线程上创建并在线程之间共享。
我发现为了性能,使用类时最好不要在线程之间共享Regex
实例。ThreadLocal
请有人解释一下为什么线程本地实例的运行速度大约快 5 倍?
以下是结果(在 8 核机器上):
Using Regex singleton' returns 3000000 and takes 00:00:01.1005695
Using thread local Regex' returns 3000000 and takes 00:00:00.2243880
源代码:
using System;
using System.Linq;
using System.Threading;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static readonly string str = new string('a', 400);
static readonly Regex re = new Regex("(a{200})(a{200})", RegexOptions.Compiled);
static void Test(Func<Regex> regexGettingMethod, string methodDesciption)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var sum = Enumerable.Repeat(str, 1000000).AsParallel().Select(s => regexGettingMethod().Match(s).Groups.Count).Sum();
sw.Stop();
Console.WriteLine("'{0}' returns {1} and takes {2}", methodDesciption, sum, sw.Elapsed);
}
static void Main(string[] args)
{
Test(() => re, "Using Regex singleton");
var threadLocalRe = new ThreadLocal<Regex>(() => new Regex(re.ToString(), RegexOptions.Compiled));
Test(() => threadLocalRe.Value, "Using thread local Regex");
Console.Write("Press any key");
Console.ReadKey();
}
}
}