我的一个方法对相同的输入有完全不同的执行时间

本文关键字:输入 执行时间 一个 方法 我的 | 更新日期: 2023-09-27 18:09:44

我有一个web服务,它应该在不到10毫秒的时间内返回答案。我有许多验证过程,包括手机号码验证。一切都很好,但我的手机号码验证执行时间是完全不同的!!有时它返回不到一毫秒,有时大约2秒!!请看看我的日志文件的一部分:(同一条目在不同的时间)

[INFO ];[2015-08-25 9:51:09,073];[11];[Intermediate_Mobile.MobileContentValidator];[1800.2534 ms]
[INFO ];[2015-08-25 10:47:53,636];[5];[Intermediate_Mobile.MobileContentValidator];[5.4598 ms]
[INFO ];[2015-08-25 10:51:09,073];[38];[Intermediate_Mobile.MobileContentValidator];[0.1379 ms]
[INFO ];[2015-08-25 11:12:09,073];[45];[Intermediate_Mobile.MobileContentValidator];[1780.4578 ms]

,这是我的方法:

public bool MobileContentValidator()
{
    Regex len = new Regex(@"^.{2,20}$"); //length at least 6 characters and maximum of 20
    Regex number = new Regex(@"^[0-9]*$"); //a string consisting only of numbers
    PhoneNumber mobile;
    Stopwatch sw = new Stopwatch();
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
    sw.Start();
    try
    {
        if (!len.IsMatch(this.Number))
        {
            this.IsNumberValid = false;
            this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MC0001");
            this.LogReason = Tools_MessageAccess.GetBriefMessage("log", "MC0001");
        }
        else if (!number.IsMatch(this.Number))
        {
            this.IsNumberValid = false;
            this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MC0001");
            this.LogReason = Tools_MessageAccess.GetBriefMessage("log", "MC0001");
        }
        else if (this.Region != "IR")
        {
            this.IsNumberValid = false;
            this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MB0003");
            this.LogReason = Tools_MessageAccess.GetBriefMessage("user", "MB0003");
        }
        else
        {
            mobile = phoneUtil.Parse(this.Number, this.Region);
            if (phoneUtil.IsValidNumber(mobile) != true)
            {
                this.IsNumberValid = false;
                this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MC0001");
                this.LogReason = Tools_MessageAccess.GetBriefMessage("log", "MC0001");
            }
            else if (phoneUtil.GetNumberType(mobile).ToString() != "MOBILE")
            {
                this.IsNumberValid = false;
                this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MU0001");
                this.LogReason = Tools_MessageAccess.GetBriefMessage("log", "MU0001");
            }
            else
                this.IsNumberValid = true;
        }
        sw.Stop();
        Tools_Log.GetLogger().Info(Tools_Log.MessageForLogFile("mobile content validation completed successfully. ", "", sw.Elapsed.TotalMilliseconds));
        return true;
    }
    catch (Exception ex)
    {
        this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "G00001");
        this.LogReason = ex.ToString();
        Tools_Log.GetLogger().Error(Tools_Log.MessageForLogFile("mobile content validation failed due to the exception in application. ", ex.ToString(), sw.Elapsed.TotalMilliseconds));
        return false;
    }
}

我知道谷歌的libphonenumber库有点重,但我不知道为什么有时需要这么长时间才能返回答案。正如我所说,我有很多验证过程,只有这一个行为奇怪。谢谢。

Update # 1: 我认为这个问题很清楚。我的坏。很抱歉。问题是为什么执行时间完全不同,我该如何修复它?我的意思是我在写作方法上有错吗?

更新# 2:更多的日志:

[INFO ];[2015-08-25 12:03:33,793];[14];[Intermediate_Mobile.ConvertNumber];[0.3712];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:33,794];[14];[Intermediate_Mobile.MobileContentValidator];[2100.12];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:33,797];[14];[Intermediate_Mobile.MobileContentValidator];[0.0761];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:33,797];[14];[Intermediate_Mobile.ConvertNumber];[0.3456];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:37,395];[14];[Intermediate_Mobile.ConvertNumber];[0.3452];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:37,395];[14];[Intermediate_Mobile.MobileContentValidator];[0.0496];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:37,398];[14];[Intermediate_Mobile.MobileContentValidator];[0.0752];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:37,399];[14];[Intermediate_Mobile.ConvertNumber];[0.3593];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,518];[11];[Intermediate_Mobile.MobileContentValidator];[0.0768];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,518];[11];[Intermediate_Mobile.ConvertNumber];[0.3776];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,518];[11];[Intermediate_Mobile.MobileContentValidator];[0.0492];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,521];[11];[Intermediate_Mobile.MobileContentValidator];[0.0966];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,521];[11];[Intermediate_Mobile.ConvertNumber];[0.383];[];[(Message: converting mobile number completed successfully. )(Exeption: )]

我的一个方法对相同的输入有完全不同的执行时间

您更新的日志显示有一个2秒的加载时间只有一次。如果您使用(较大的)外部库,这(有点)正常,可能需要在第一次调用时加载一些程序集。您不应该在以下具有相同输入(因此通过库的控制流相同)的调用中看到这样的延迟

虽然我不清楚你的问题的确切解决方案,但你可以通过编译一次RegEx并始终使用它并保持它静态来提高性能。然后使用静态编译的RegEx对象。