簡體   English   中英

基准化.Net正則表達式選項-或RightToLeft做什么?

[英]Benchmarking .Net regex options - or what does RightToLeft do?

我創建了一個演示應用程序來測試某些正則表達式的性能。 我的第三個測試使用選項RightToLeft

看來它大大加快了流程! 為什么? 它有什么作用?

這是我的測試應用程序:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        private const string IsRequestForDirectoryWithoutTrailingSlashRegex = @"^(?#Path:)(.*/)?(?#LastPart:)(?<!\.asmx|\.aspx/)([^./?#]+?)(?#QueryString:)(\?.*?)(?#Anchor:)?(#.*)?$";

        private static string[] Tests = new string[] {
            "http://localhost/manager/page.aspx",
            "http://localhost/manager/",
            "http://localhost/manager",
            "http://localhost/manager/?param=value",
            "http://localhost/manager/dir?param=value"
        };

        static void Main(string[] args)
        {
            Test1();
            Test2();
            Test3();
            Test4();

            Console.WriteLine();
            Console.ReadLine();
        }

        public static void Test1()
        {
            Regex regex = new Regex(IsRequestForDirectoryWithoutTrailingSlashRegex);
            DoWork("1", regex);
        }

        public static void Test2()
        {
            Regex regex = new Regex(IsRequestForDirectoryWithoutTrailingSlashRegex, RegexOptions.Compiled);
            DoWork("2", regex);
        }

        public static void Test3()
        {
            Regex regex = new Regex(IsRequestForDirectoryWithoutTrailingSlashRegex, RegexOptions.Compiled | RegexOptions.RightToLeft);
            DoWork("3", regex);
        }

        public static void Test4()
        {
            Regex regex = new Regex(IsRequestForDirectoryWithoutTrailingSlashRegex, RegexOptions.Compiled | RegexOptions.RightToLeft | RegexOptions.IgnoreCase);
            DoWork("4", regex);
        }
        static void DoWork(string name, Regex regex)
        {
            Stopwatch sp = new Stopwatch();
            sp.Start();

            for (int i = 0; i < 100000; i++)
            {
                foreach (string s in Tests)
                {
                    regex.IsMatch(s);
                }
            }

            foreach (string s in Tests)
            {
                Console.WriteLine(":" + s + ":" + regex.IsMatch(s).ToString());
            }

            sp.Stop();

            Console.WriteLine("Test " + name + ": " + sp.ElapsedTicks);
        }
    }
}

RegexOptions.RightToLeft在嘗試匹配您希望在輸入字符串末尾找到的模式時非常有用,因為正如其文檔所述:搜索來自 右到左 從輸入字符串的最后一個字符開始,從左到右,雖然regex本身仍然從左到右應用。

您的正則表達式似乎正在尋找目錄路徑的尾部斜杠,因此這似乎是符合說明的情況。

盡管您的表達式正在尋找斜杠,但是這兩個錨點^$ )的存在使我的推理錯了,因為正則表達式無論從何處開始都只能以一種可能的方式進行匹配。

我將繼續尋找導致此問題的實際原因,但現在我將保持原樣。

另一方面,表達式(?#Path:)之后的表達式.*/部分會消耗整個輸入字符串,然后每次查找最后一個/時都會遞歸返回,因此在啟動時進一步搜索可能沒有很多回溯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM