Switch String

  • Switch string
    • Java 7新特性,原来switch只能支持int,byte。short,char,枚举
  • 那具体性能如何了?看下code
package com.wilson.test;
public class SwitchTest {
    public static final String NUM_ONE = "1";
    public static final String NUM_TWO = "2";
    public static final String NUM_THREE = "3";
    public static final String NUM_FOUR = "4";
    public static final String NUM_FIVE = "5";
    public static final String NUM_SIX = "6";
    public static final String NUM_SEVEN = "7";
    public static final String NUM_EIGHT = "8";

    public static void main(String[] args) {
        System.out.println("1->switchTime:"+switchOutput("1")+",ifElse:"+ifElseOutput("1"));
        System.out.println("2->switchTime:"+switchOutput("2")+",ifElse:"+ifElseOutput("2"));
        System.out.println("3->switchTime:"+switchOutput("3")+",ifElse:"+ifElseOutput("3"));
        System.out.println("4->switchTime:"+switchOutput("4")+",ifElse:"+ifElseOutput("4"));
        System.out.println("5->switchTime:"+switchOutput("5")+",ifElse:"+ifElseOutput("5"));
        System.out.println("6->switchTime:"+switchOutput("6")+",ifElse:"+ifElseOutput("6"));
        System.out.println("7->switchTime:"+switchOutput("7")+",ifElse:"+ifElseOutput("7"));
        System.out.println("8->switchTime:"+switchOutput("8")+",ifElse:"+ifElseOutput("8"));
    }

    public static long switchOutput(String num) {
        long start = System.currentTimeMillis();
        if (num == null || num.equals("")) {
            return 0;
        }
        for (int i = 0; i < 1000000000; i++) {
            switch (num) {
                case NUM_ONE:
                    break;
                case NUM_TWO:
                    break;
                case NUM_THREE:
                    break;
                case NUM_FOUR:
                    break;
                case NUM_FIVE:
                    break;
                case NUM_SIX:
                    break;
                case NUM_SEVEN:
                    break;
                case NUM_EIGHT:
                    break;
            }
        }

        long end = System.currentTimeMillis();
        return end - start;
    }

    public static long ifElseOutput(String num) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            if (num.equals(NUM_ONE)) {
                break;
            } else if (num.equals(NUM_TWO)) {
                break;
            } else if (num.equals(NUM_THREE)) {
                break;
            } else if (num.equals(NUM_FOUR)) {
                break;
            } else if (num.equals(NUM_FIVE)) {
                break;
            } else if (num.equals(NUM_SIX)) {
                break;
            } else if (num.equals(NUM_SEVEN)) {
                break;
            } else if (num.equals(NUM_EIGHT)) {
                break;
            }
        }

        long end = System.currentTimeMillis();
        return end - start;
    }
}

开始想当然switch速度更快,那实际输出了?

1->switchTime:1868,ifElse:0
2->switchTime:2381,ifElse:0
3->switchTime:985,ifElse:0
4->switchTime:1002,ifElse:0
5->switchTime:486,ifElse:0
6->switchTime:662,ifElse:0
7->switchTime:652,ifElse:0
8->switchTime:661,ifElse:0

结果大出我所希望的。用"if-if-else"几乎完胜switch String,那为什么了?
可以看下这篇文章,主要switch string,是通过equal和hashcode实现的。String先通过hashcode转换为int,在通过switch int,然后子case里面在if equal String。

  • 看来还是回来老路,只是添加一个新的支持特性而效率没有提高。对应String还是用if-if-else吧
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,982评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,776评论 0 33
  • 沟通是什么? 沟通是一座桥梁, 架接你我的思想; 沟通是一根纽带, 系紧你我的心灵; 沟通是一捧火焰, 融化矛盾的...
    快乐灵芝阅读 610评论 14 21
  • 与你们相遇,太幸运 我一直在酝酿,我要怎样提笔写下这篇文章,要怎样写得情深意切读来催人泪下,事实上我特别害怕到最后...
    涛娃涛阅读 289评论 0 0