使用正则表达式和原生代码哪个比较快

内容纲要

示例:
判断字符串是否由字母数字下划线组成:

using System;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace ConsoleApp6
{
    class Program
    {
        static Regex regex = new Regex(@"^[A-Za-z0-9]+$");
        static void Main(string[] args)
        {
            string[] a = new string[] {
                "Absdbkbs",
                "asdf4642`",
                "fsdg4654fs4",
                "r23fwegf34",
                "e23rwef,",
                "fewfsg35453453",
                "545345415",
                "dalsjfiose2",
                "dasfwetr23`",
                "dsaf5454-"};
            System.Threading.Thread.Sleep(5000);
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < 1_0_0000; i++)
            {
                for (int k = 0; k < a.Length; k++)
                {
                    var tmp = IsNumAndEnCh(a[k]);
                }
            }
            watch.Stop();
            Console.WriteLine("执行10 0000 * 10 正则表达式验证:" + watch.ElapsedMilliseconds);

            watch.Restart();

            for (int i = 0; i < 1_0_0000; i++)
            {
                for (int k = 0; k < a.Length; k++)
                {
                    var tmp = IsHas(a[k]);
                }
            }
            watch.Stop();
            Console.WriteLine("执行10 0000 * 10 C#代码验证:" + watch.ElapsedMilliseconds);
            Console.ReadKey();
        }
        /// <summary>
        /// 判断输入的字符串是否只包含数字和英文字母
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumAndEnCh(string input)
        {
            return regex.IsMatch(input);
        }
        public static bool IsHas(string input)
        {
            ReadOnlySpan<char> span = input.AsSpan();
            for (int i = 0; i < span.Length; i++)
            {
                if (!char.IsLetterOrDigit(span[i]))
                    return false;
            }
            return true;
        }
    }
}

输出结果是

执行10 0000 * 10 正则表达式验证:986
执行10 0000 * 10 C#代码验证:237

原生代码确实比正则表达式快。

点赞
  1. I am glad for writing to make you know what a magnificent encounter my friend's girl gained studying your web site. She learned several pieces, which include what it's like to possess an awesome helping nature to let other folks very easily know just exactly several multifaceted subject matter. You truly exceeded people's expectations. Thank you for presenting these powerful, safe, explanatory and in addition fun tips about this topic to Kate.

  2. cbd oil说道:

    I needed to create you one little observation just to say thank you as before for these fantastic information you've discussed here. It has been certainly particularly generous of people like you to convey freely all that a lot of people could possibly have made available as an e book to earn some cash for themselves, most notably given that you could possibly have done it if you ever decided. These points as well served to be a fantastic way to fully grasp other people have similar fervor much like my own to know the truth lots more pertaining to this issue. I am certain there are millions of more fun opportunities in the future for individuals who looked at your blog post.

  3. I am commenting to let you know what a nice encounter my wife's girl had studying your web site. She mastered several pieces, which include what it's like to possess an awesome helping nature to let other individuals very easily know precisely some problematic subject matter. You truly exceeded people's expectations. Thank you for presenting these practical, trusted, informative and in addition fun tips about this topic to Kate.

  4. Regards for all your efforts that you have put in this. Very interesting info.

发表评论

邮箱地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!