Android中split,java.util.StringTokenizer和indexOf方法效率的对比,不看不知道,一看下一跳

这几天在给droidwall防火墙添加新功能时遇到了一点点算法问题,要拆分个字符串,想找个效率高点的方法,从网上搜了下,有的说split是sun推荐的,所以用split;有的说StringTokenizer的效率更高,也看到好多实验的代码,于是我针对android修改了下,运行(android2.2虚拟机上)后真是下一跳,split不是慢得一星半点!

下面是测试代码:

Text Code
1 package a.demo;
2
3  import java.util.*;
4
5  import android.app.Activity;
6  import android.os.Bundle;
7
8  public class S extends Activity {
9 @Override
10 protected void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 setContentView(R.layout.main);
13 String str = "zkw";
14 StringBuffer buffer = new StringBuffer();
15
16 // 准备好字符串,总共有1000个“zkw”,以“,”相隔
17 for (int i = 0; i < 1000; i++) {
18 buffer.append(str).append(",");
19 }
20 str = buffer.toString();
21
22 //String.split()
23 splitTime(str);
24
25 //List+indexOf()
26 listTime(str);
27
28 //String[]+indexof()
29 arrayTime(str);
30
31 //StringTokenizer
32 tokenizer(str);
33
34 }
35
36 private void splitTime(String str) {
37 long curTime = System.currentTimeMillis();
38 String[] array = null;
39 for (int m = 0; m < 100; m++) {
40 array = str.split(",");
41 }
42 //printArray(array);
43 System.out.println("String.split用时 : "
44 + (System.currentTimeMillis() - curTime));
45 }
46
47 private void listTime(String str) {
48 long curTime = System.currentTimeMillis();
49 String[] array = null;
50 for (int n = 0; n < 100; n++) {
51 ArrayList<String> arrayList = new ArrayList<String>();
52 int index = 0, offset = 0;
53 while ((index = str.indexOf(",", index + 1)) != -1) {
54 arrayList.add(str.substring(offset, index));
55 offset = index + 1;
56 }
57 array = arrayList.toArray(new String[0]);
58 }
59 //printArray(array);
60 System.out.println("List+indexOf()用时 : "
61 + (System.currentTimeMillis() - curTime));
62 }
63
64 private void arrayTime(String str) {
65 long curTime = System.currentTimeMillis();
66 String[] array = new String[1001];
67 for (int n = 0; n < 100; n++) {
68 int index = 0, offset = 0;
69 int i = 0;
70 while ((index = str.indexOf(",", index + 1)) != -1) {
71 array[i] = str.substring(offset, index);
72 offset = index + 1;
73 i++;
74 }
75 array[i] = str.substring(offset);
76 }
77 //printArray(array);
78 System.out.println("String[]+indexOf()用时 : "
79 + (System.currentTimeMillis() - curTime));
80 }
81
82 private void tokenizer(String str) {
83 long curTime = System.currentTimeMillis();
84 String[] array = null;
85 for (int m = 0; m < 100; m++) {
86 StringTokenizer token = new StringTokenizer(str, ",");
87 array = new String[token.countTokens()];
88 int i = 0;
89 while (token.hasMoreTokens()) {
90 array[i++] = token.nextToken();
91 }
92 }
93 //printArray(array);
94 System.out.println("StringTokener用时 : "
95 + (System.currentTimeMillis() - curTime));
96 }
97
98 /*
99 * 检验每个array数组是否相等
100 * 仅仅是打印
101 */
102 private void printArray(String[] array) {
103 for (int i = 0; i < array.length; i++) {
104 System.out.print(array[i]);
105 }
106 System.out.println("");
107 }
108 }

中间两个是自己简单实现的算法,如果专门设计个算法,速度肯定更快!


PS:以前用Python的时候对split()印象挺好的,到了JAVA上发现很少有人用,今天测试才发现是有原因的。

PS2:这个测试仅仅在android平台上有效,在PC上还是split()比StringTokenizer稍微快点,仅仅是稍微,而且还是没有自己实现的算法快...

posted @ 2011-05-20 20:36  周柯文  阅读(4566)  评论(0编辑  收藏  举报