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

穆罕默德

我有一个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;
    }
}

我知道Google的libphonenumber图书馆有点沉重,但我不知道为什么有时花这么长时间才能返回答案。正如我所说的,我有很多验证过程,只是这一行为很奇怪。谢谢。

更新#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秒。如果使用(较大的)外部库,这是(某种)正常现象,这在第一次调用时可能需要加载某些程序集。在具有相同输入的后续调用中,您应该不会看到这样的延迟(因此,通过库的控制流也相同)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

具有不同执行时间的异步流

相同请求的执行时间不同

查询执行时间是否因具有巨大表的不同查询参数值而不同?

为什么相同 SQL 查询的执行时间不同?

为什么相同的python块的执行时间不同?

同一操作在两个相同项目中的执行时间不同

具有几乎不同的执行时间的几乎相等的Cypher查询

SQL中基于where条件的不同时间的相同执行时间?我该如何解决?

具有步骤执行时间限制的For循环

我如何在同一图中绘制具有完全不同含义的倍数列

为什么CHECKSUM_AGG()对于完全不同的输入值返回相同的值?

相同的SQL Server查询执行时间不同(其中语句相同但语法不同)

相同查询的执行时间不同,where子句中的值不同

在boost :: asio中,可以有一个具有多个执行时间的datetime_timer吗?

共享主机具有固定的最大执行时间;我的管理员php脚本超出了

在不同的计算机上相同的算法执行时间?

不同线程数 openMP 上的相同程序执行时间

跟踪方法执行时间

计算方法的执行时间

执行时间

在Golang中以最小间隔调用具有可变执行时间的函数的最佳方法?

具有和不具有参数C / C ++的函数之间的执行时间

有没有一种有效的方法来计算golang中的执行时间?

提交完全不同的IOS App,但具有相同的捆绑包ID

线性搜索和反向线性搜索没有相同的执行时间

相同的视图,非常不同的执行时间(是:Oracle 链接服务器很慢,但只有第二次)

为什么程序执行时间与以前相同?

为什么在mySql过程执行中会有一段执行时间

具有SVM基本分类器的AdaBoost的执行时间