加入收藏 | 设为首页 | 会员中心 | 我要投稿 我爱故事小小网_铜陵站长网 (http://www.0562zz.com/)- 视频终端、云渲染、应用安全、数据安全、安全管理!
当前位置: 首页 > 教程 > 正文

Android 判断字符串是否为空的最优技巧

发布时间:2021-12-13 16:22:51 所属栏目:教程 来源:互联网
导读:在Android 的开发中经常会使用判断字符串是否为空,虽然现在智能手机的运行速度越来越高,但为了使应用更加的流畅,我们应该保证 在编写程序时使用较好的方法来提高效率。一下为四种方法的运行时间的比较: public class TestEmptyString { String s = ; lon

在Android 的开发中经常会使用判断字符串是否为空,虽然现在智能手机的运行速度越来越高,但为了使应用更加的流畅,我们应该保证
 
在编写程序时使用较好的方法来提高效率。一下为四种方法的运行时间的比较:
 
public class TestEmptyString {
   String  s  = "";
   long    n  = 10000000;
   private void function1() {
   long startTime = System.currentTimeMillis();
   for (long i = 0; i < n; i++) {
   if (s == null || s.equals(""))
   ;
   }
   long endTime = System.currentTimeMillis();
   System.out.println("function 1 use time: " + (endTime - startTime)
   + "ms");
   }
   private void function2() {
   long startTime = System.currentTimeMillis();
   for (long i = 0; i < n; i++) {
   if (s == null || s.length() <= 0)
   ;
   }
   long endTime = System.currentTimeMillis();
   System.out.println("function 2 use time: " + (endTime - startTime)
   + "ms");
   }
   private void function3() {
   long startTime = System.currentTimeMillis();
   for (long i = 0; i < n; i++) {
   if (s == null || s.isEmpty())
   ;
   }
   long endTime = System.currentTimeMillis();
   System.out.println("function 3 use time: " + (endTime - startTime)
   + "ms");
   }
   private void function4() {
   long startTime = System.currentTimeMillis();
   for (long i = 0; i < n; i++) {
   if (s == null || s == "")
   ;
   }
   long endTime = System.currentTimeMillis();
   System.out.println("function 4 use time: " + (endTime - startTime)
   + "ms");
   }
   public static void main(String[] args) {
   TestEmptyString test = new TestEmptyString();
   test.function1();
   test.function2();
   test.function3();
   test.function4();
   }
 
 

(编辑:我爱故事小小网_铜陵站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读