StringTokenizer最快 ,基本已经不用了,除非在某些需要效率的场合。
Scanner最慢。
String和Pattern速度差不多。Pattern稍快些。 String和Pattern的split 方法效率相当,常用
- public static void main(String [] args){
- long start = System.currentTimeMillis();
- for ( int i= 0 ;i< 100000 ;i++){
- test1();
- }
- long end = System.currentTimeMillis();
- System.out.println("StringTokenzier: " +(end-start));
- start = System.currentTimeMillis();
- for ( int i= 0 ;i< 100000 ;i++){
- test2();
- }
- end = System.currentTimeMillis();
- System.out.println("Scanner: " +(end-start));
- start = System.currentTimeMillis();
- for ( int i= 0 ;i< 100000 ;i++){
- test3();
- }
- end = System.currentTimeMillis();
- System.out.println("String: " +(end-start));
- start = System.currentTimeMillis();
- for ( int i= 0 ;i< 100000 ;i++){
- test4();
- }
- end = System.currentTimeMillis();
- System.out.println("Pattern: " +(end-start));
- }
- //用StringTokenizer
- public static void test1(){
- String input = "This is 1 just a test" ;
- StringTokenizer st = new StringTokenizer(input);
- while (st.hasMoreTokens()){
- String s = st.nextToken();
- }
- }
- //用Scanner的方法
- public static void test2(){
- String input = "This is 1 just a test" ;
- Scanner scanner = new Scanner(input);
- scanner.useDelimiter(" " );
- while (scanner.hasNext()){
- String s = scanner.next();
- }
- }
- //用String的split方法
- public static void test3(){
- String input = "This is 1 just a test" ;
- String [] str = input.split(" " );
- for ( int i= 0 ;i<str.length;i++){
- String s = str[i];
- }
- }
- //用Pattern的方法
- public static void test4(){
- Pattern pattern = Pattern.compile(" " );
- String input = "This is 1 just a test" ;
- String [] str = pattern.split(input);
- for ( int i= 0 ;i<str.length;i++){
- String s = str[i];
- }
- }
- public static void main(String [] args){
- long start = System.currentTimeMillis();
- for(int i=0;i<100000;i++){
- test1();
- }
- long end = System.currentTimeMillis();
- System.out.println("StringTokenzier: "+(end-start));
- start = System.currentTimeMillis();
- for(int i=0;i<100000;i++){
- test2();
- }
- end = System.currentTimeMillis();
- System.out.println("Scanner: "+(end-start));
- start = System.currentTimeMillis();
- for(int i=0;i<100000;i++){
- test3();
- }
- end = System.currentTimeMillis();
- System.out.println("String: "+(end-start));
- start = System.currentTimeMillis();
- for(int i=0;i<100000;i++){
- test4();
- }
- end = System.currentTimeMillis();
- System.out.println("Pattern: "+(end-start));
- }
- //用StringTokenizer
- public static void test1(){
- String input = "This is 1 just a test";
- StringTokenizer st = new StringTokenizer(input);
- while(st.hasMoreTokens()){
- String s = st.nextToken();
- }
- }
- //用Scanner的方法
- public static void test2(){
- String input = "This is 1 just a test";
- Scanner scanner = new Scanner(input);
- scanner.useDelimiter(" ");
- while(scanner.hasNext()){
- String s = scanner.next();
- }
- }
- //用String的split方法
- public static void test3(){
- String input = "This is 1 just a test";
- String [] str = input.split(" ");
- for(int i=0;i<str.length;i++){
- String s = str[i];
- }
- }
- //用Pattern的方法
- public static void test4(){
- Pattern pattern = Pattern.compile(" ");
- String input = "This is 1 just a test";
- String [] str = pattern.split(input);
- for(int i=0;i<str.length;i++){
- String s = str[i];
- }
- }
运行了五次。输出结果如下:
- StringTokenzier: 110
- Scanner: 3281
- String: 453
- Pattern: 438
- StringTokenzier: 109
- Scanner: 3297
- String: 453
- Pattern: 438
- StringTokenzier: 110
- Scanner: 3265
- String: 485
- Pattern: 437
- StringTokenzier: 109
- Scanner: 3266
- String: 469
- Pattern: 437
- StringTokenzier: 109
- Scanner: 3297
- String: 469
- Pattern: 422
- StringTokenzier: 110
- Scanner: 3281
- String: 453
- Pattern: 438
- StringTokenzier: 109
- Scanner: 3297
- String: 453
- Pattern: 438
- StringTokenzier: 110
- Scanner: 3265
- String: 485
- Pattern: 437
- StringTokenzier: 109
- Scanner: 3266
- String: 469
- Pattern: 437
- StringTokenzier: 109
- Scanner: 3297
- String: 469
- Pattern: 422
StringTokenizer最快
Scanner最慢。
String和Pattern速度差不多。Pattern稍快些。