substring,subsequence,charAt执行效率的不同

2018-09-18 06:31:55来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

 1 package com.java.tencent;
 2 
 3 public class T_2_longestPalindrome {
 4 
 5     public String test1(String s){
 6         long startTime=System.currentTimeMillis();
 7         int len = s.length();
 8         int tmp = 0;
 9         String result = "";
10         for(int i=0;i<len;i++){
11             if(tmp+i-len>0){
12                 break;
13             }
14             int lenj = len-1;
15             for(int j=lenj;j>=i;j--){
16                 if(tmp>j-i+1){
17                     break;
18                 }
19                 String ch = s.substring(i,i+1);
20                 String chj = s.substring(j,j+1);
21                 if(ch.equals(chj)){
22                     String str = s.substring(i,j+1);
23                     if(tmp>str.length()){
24                         continue;
25                     }
26                     boolean bl = true;
27                     int ln = str.length();
28                     int lenm = ln/2+1;
29                     for(int m=0;m<lenm;m++){
30                         CharSequence start = str.subSequence(m, m+1);
31                         CharSequence end = str.subSequence(ln-m-1,ln-m);
32                         if(!start.equals(end)){
33                             bl = false;
34                             break;
35                         }
36                     }
37                     if(bl && ln>tmp){
38                         result = str;
39                         tmp = ln;
40                     }
41                 }
42             }
43         }
44         System.out.println(result);
45         long endTime=System.currentTimeMillis();
46         System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
47         return result;
48     }
49 
50     public String test2(String s){
51         long startTime=System.currentTimeMillis();
52         int len = s.length();
53         int tmp = 0;
54         String result = "";
55         for(int i=0;i<len;i++){
56             if(tmp+i-len>0){
57                 break;
58             }
59             for(int j=len-1;j>=i;j--){
60                 if(tmp>j-i+1){
61                     break;
62                 }
63                 String str = s.substring(i,j+1);
64                 boolean bl = true;
65                 for(int m=0;m<(str.length()/2+1);m++){
66                     Character start = str.charAt(m);
67                     Character end = str.charAt(str.length()-m-1);
68                     if(!start.equals(end)){
69                         bl = false;
70                         break;
71                     }
72                 }
73                 if(bl && str.length()>tmp){
74                     result = str;
75                     tmp = str.length();
76                     break;
77                 }
78             }
79         }
80         System.out.println(result);
81         long endTime=System.currentTimeMillis();
82         System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
83         return result;
84     }
85     
86     
87     public static void main(String[] args) {
88         T_2_longestPalindrome lp = new T_2_longestPalindrome();
89         String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
90         lp.test2(s);
91     }
92 
93 }

以上写了一个返回最长回文子串的程序(1000个a,回文字串就是如abcddcba、bcb,左右两边相同,当然a一个字符也是),之前使用的test1总是500以上的执行时间,时间总是无法降低,最后问题落到了两个截取字串位置上,经过修改,test2成功降低执行时间。

test1 使用 sub 截取字符串,执行时间500+ms

test2 使用 charAt,执行时间180ms

以上可以明显看出执行效率的不同。

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:使用命令把类打成jar包

下一篇:spring-boot-2.0.3不一样系列之源码篇 - SpringApplication的run