zoukankan      html  css  js  c++  java
  • Android正则表达式使用及性能隐患分析

    场景:找出一个关键词在一条短信中出现的次数

    使用正则的实现方式:

    public static int findKeyWordCount(String srcText, String keyword) {
        int count = 0;
        Pattern p = Pattern.compile(keyword);
        Matcher m = p.matcher(srcText);
        while (m.find()) {
            count++;
        }
        return count;
    }    

    普通方式:

    public static int getSubCount(String str, String key) {  
        int count = 0;  
        int index = 0;  
        while ((index = str.indexOf(key, index)) != -1) {  
            index = index + key.length();  
            count++;  
        } 
        return count;
    }    

    第一种方式耗时可能会出现百毫秒级别的情况,这种情况跟正则表达式的复杂情况有关,使用的时候留意一下,打log日志跟踪时间

    第二种是几毫秒级别的,如果有性能方面的考虑,请小心使用

  • 相关阅读:
    TP框架实现分页及条件查询
    tp框架连贯操作
    php查询
    php修改数据
    php增加数据处理
    php删除数据
    php怎么访问数据库
    php查询
    克隆及加载类
    php静态成员和接口
  • 原文地址:https://www.cnblogs.com/popfisher/p/5212518.html
Copyright © 2011-2022 走看看