zoukankan      html  css  js  c++  java
  • java--split,index,StringTokenizer比较

    import java.util.StringTokenizer;
    
    public class SplitDemo {
        //jdk8
        public static void main(String[] args){
            String s = "a,b,c";
            String[] split = s.split(",");
            for (String s1 : split) {
                System.out.println(s1);
            }
            StringTokenizer st = new StringTokenizer(s,",");
            while (st.hasMoreTokens()){
                String s1 = st.nextToken();
                System.out.println(s1);
            }
            String s2 = "a,b,c";
            while (true){
                int i = s2.indexOf(",");
                if (i < 0 && s2.length()<0){
                    break;
                }
                if (i < 0){
                    System.out.println(s2);
                    break;
                }else {
                    System.out.println(s2.substring(0,i));
                    s2 = s2.substring(i+1);
                }
            }
    
            testSplit();
            testStringTokenizer();
            testIndexOf();
            //split-598
            //StringTokenizer-38
            //indexOf-4577
        }
        public static void testSplit(){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 1000; i++) {
                sb.append(i);
                if (i < 999){
                    sb.append(",");
                }
            }
            String str = sb.toString();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                str.split(",");
            }
            long end = System.currentTimeMillis();
            System.out.println("split" + (end-begin));
        }
        public static void testStringTokenizer(){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 1000; i++) {
                sb.append(i);
                if (i < 999){
                    sb.append(",");
                }
            }
            String str = sb.toString();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                StringTokenizer st = new StringTokenizer(str);
                while (st.hasMoreTokens()){
                    st.nextToken();
                }
                st = new StringTokenizer(str);
            }
            long end = System.currentTimeMillis();
            System.out.println("StringTokenizer" + (end-begin));
        }
        public static void testIndexOf(){
            String s = null;
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 1000; i++) {
                sb.append(i);
                if (i < 999){
                    sb.append(",");
                }
            }
            s = sb.toString();
            String str = s;
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                while (true){
                    int a = str.indexOf(",");
                    if (a < 0){
                        break;
                    }
                    str = str.substring(a+1);
                }
                str = s;
            }
            long end = System.currentTimeMillis();
            System.out.println("indexOf" + (end-begin));
        }
    }
    
  • 相关阅读:
    web.config中的customErrors标记的用法
    算法系列15天速成[索引]
    log4net的简单使用
    Cookie帮助类
    ASPxTreeList及ASPxGridView使用
    javascript获取页面中的位置
    如何部署windows服务?
    基于T4模板引擎生成静态网站(CMS)
    SqlServer实现递归查询
    安卓(AndRoid)开发环境搭建之HelloWord
  • 原文地址:https://www.cnblogs.com/fly-book/p/11340420.html
Copyright © 2011-2022 走看看