zoukankan      html  css  js  c++  java
  • 正则表达式与模版解析的性能比较


                
                ---正则表达式与模版解析的性能比较
            //使用正则表达式获取需要的货号
            //对于企业工单导入时对工单表头中的货号是否存在于工单工作表与历史表中进行验证
            private  bool IsVerfiyHeadItemNo(string templateName)
            {
                bool result=true;
                using(FormatConvert.EPDAO.TxtConnection  conn=new FormatConvert.EPDAO.TxtConnection (this.ImportFile.FullName))
                {
                    #region 工单表头中的货号验证
                    conn.Open();
                    using(StreamReader sr = new StreamReader( conn.Stream,ConfigHelper.TxtCoding))
                    {
                        conn.Stream.Position = 0;
                        string txt= "";                    
                        int linesum = 0;                    
                        while((txt = sr.ReadLine()) != null)
                        {                        
                            if (!IsHead(txt))
                            {
                                continue;
                            }                            
                            string itemno=GetItemNo(txt);
                            linesum++;                    
                            if(    itemno!=string.Empty)
                            {                                                        
                                result=this.IsUnique(itemno,linesum);        
                                if (result==false)
                                {                            
                                    this.ImportFile.CopyTo(Constants.g_ownListErrorPath+"\\"+this.ImportFile.Name,true);//备份文件.
                                    return result;
                                }                            
                            }
                        }                    
                        #endregion
                    }
                }return result;
            }
            
            //根据模版解析当前报文的行(按行解析并返回对象)
            //对于企业工单导入时对工单表头中的货号是否存在于工单工作表与历史表中进行验证
            private  bool IsVerfiyHeadItemNoLine(string templateName)
            {
                bool result=true;
                using(FormatConvert.EPDAO.TxtConnection  conn=new FormatConvert.EPDAO.TxtConnection (this.ImportFile.FullName))
                {
                    #region 工单表头中的货号验证
                    conn.Open();
                    TxtTemplate template = TemplateFactory.GetTemplate(Ems_Edi_Wo_HeadDao.templatePath,
                        templateName,typeof(Ems_Edi_Wo_HeadDao.DB_COLS),typeof(EMS_EDI_WO_HEAD.COLS),typeof(EMS_EDI_WO_HEAD));        
                    using(StreamReader sr = new StreamReader( conn.Stream,ConfigHelper.TxtCoding))
                    {
                        conn.Stream.Position = 0;
                        string txt= "";                    
                        TxtMessage msg=new TxtMessage();
                        int linesum = 0;                    
                        while((txt = sr.ReadLine()) != null)
                        {                        
                            if (!IsHead(txt))
                            {
                                continue;
                            }                            
                            TxtLine line=new TxtLine(txt);
                            msg.AddLine(line);
                            IList tmpWo_head=template.TranslateTxt(msg);
                            EMS_EDI_WO_HEAD head=tmpWo_head[0] as EMS_EDI_WO_HEAD;
                            string itemno=head.Item_No;
                            linesum++;                    
                            if(    itemno!=string.Empty)
                            {                                                        
                                result=this.IsUnique(itemno,linesum);        
                                if (result==false)
                                {                                
                                    return result;
                                }                            
                            }
                            msg.clear();
                        }                    
                        #endregion
                    }
                }return result;
            }


            //实体类赋值
            /// <summary>
            /// 用模版解析当前报文的行(按行解析并返回对象)
            /// </summary>
            /// <param name="line">报文行对象</param>
            /// <returns></returns>
            public IModel TranslateLine(TxtLine line)
            {            
                string txtbody = line.Txtbody;
                object model = GetModelInstance();        
                SetModelProperty(model,txtbody);            
                return (IModel)model;
            }    
            
            /// <summary>
            /// 将报文中当前行的信息放入model中
            /// </summary>
            /// <param name="model"></param>
            /// <param name="txt"></param>
            private void SetModelProperty(object model,string txt)
            {            
                for(int i=1;i<dt.Rows.Count;i++)
                {                
                    int length = Convert.ToInt32(dt.Rows[i]["FIELDLENGTH"]);
                    string name = dt.Rows[i]["ColName"].ToString();                                    
                    string val = GetEncodingString(txt,length);    
                    name = NameCol2Prop(name).ToString();  //根据Dao中定义的数据库枚举返回Model成员变量的枚举值
                    SetValue(model,name,val);        
                    txt = CutStringByLength(txt,length);            
                }                    
            }


    ----------正则判断---------

    模板解析验证共644条记录
    第一次:费时0分0秒546毫秒

    第二次:费时0分0秒500毫秒
    第三次:费时0分0秒500毫秒
    第四次:费时0分0秒515毫秒
    第五次:费时0分0秒578毫秒
    -------------------





    正则解析验证共644条记录
    第一次:费时0分0秒406毫秒
    第二次:费时0分0秒390毫秒

    第三次:费时0分0秒390毫秒
    第四次:费时0分0秒390毫秒
    第五次:费时0分0秒406毫秒


    模板解析/正则解析
    2639/1982=133.15%
    正则解析/模板解析
    1982/2639=75.10%

  • 相关阅读:
    矩阵快速幂
    BITSET
    不再以讹传讹,GET和POST的真正区别(转)
    Anaconda 镜像
    Anaconda常用命令大全
    如何使用抓包工具fiddler对app进行接口分析
    windows10搭建django1.10.3+Apache2.4
    如何用jenkins实现自动化构建新版本和二维码下载
    解决从jenkins打开robot framework报告会提示‘Opening Robot Framework log failed ’的问题
    Macaca开源--阿里的移动自动化测试框架
  • 原文地址:https://www.cnblogs.com/chillsrc/p/2313691.html
Copyright © 2011-2022 走看看