<input id="ohw05"></input>
  • <table id="ohw05"><menu id="ohw05"></menu></table>
  • <var id="ohw05"></var>
  • <code id="ohw05"><cite id="ohw05"></cite></code>
    <label id="ohw05"></label>
    <var id="ohw05"></var>
  • arctic_fox

    貴有恒何必三更眠五更起 最無益只怕一日曝十日寒

      博客園  :: 首頁  :: 新隨筆  :: 聯系 :: 訂閱 訂閱  :: 管理

     

    常用的四種循環,普通for-增強for-forEach-forEach-Stream循環  是否真正思考過他們的不同

    實踐出真知,讓我們從實踐著手

    public class ForDemo {
    public static void main(String[] args) {

    Integer initial = 10;

    for (int n = 1; n < 6; n++) {
    System.out.println("-----------------------------------------");

    List<String> sourceList = new ArrayList<>();
    for (int i = 0; i < initial; i++) {
    sourceList.add("第" + i + "條數據");
    }
    compareCycle(sourceList);

    initial *= 10;
    sourceList = null;
    }
    }

    public static void compareCycle(List<String> sourceList) {
    System.out.println("數據條數:" + sourceList.size());
    long a1 = System.currentTimeMillis();
    for (int i = 0; i < sourceList.size(); i++) doSome();
    long a2 = System.currentTimeMillis();
    System.out.println("普通for循環用時:" + (a2 - a1) + " ms");

    long b1 = System.currentTimeMillis();
    for (String t : sourceList) doSome();
    long b2 = System.currentTimeMillis();
    System.out.println("增強for循環用時:" + (b2 - b1) + " ms");

    long c1 = System.currentTimeMillis();
    sourceList.forEach((t) -> doSome());
    long c2 = System.currentTimeMillis();
    System.out.println("forEach循環用時:" + (c2 - c1) + " ms");

    long d1 = System.currentTimeMillis();
    sourceList.stream().forEach((t) -> doSome());
    long d2 = System.currentTimeMillis();
    System.out.println("forEach-Stream循環用時:" + (d2 - d1) + " ms");

    long e1 = System.currentTimeMillis();
    sourceList.parallelStream().forEach((t) -> doSome());
    long e2 = System.currentTimeMillis();
    System.out.println("forEach-parallelStreamm循環用時:" + (e2 - e1) + " ms");
    }

    private static void doSome() {
    try {
    Thread.sleep(1);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

      運行結果:

    -----------------------------------------
    數據條數:10
    普通for循環用時:13 ms
    增強for循環用時:13 ms
    forEach循環用時:133 ms
    forEach-Stream循環用時:15 ms
    forEach-parallelStreamm循環用時:5 ms
    -----------------------------------------
    數據條數:100
    普通for循環用時:130 ms
    增強for循環用時:126 ms
    forEach循環用時:128 ms
    forEach-Stream循環用時:126 ms
    forEach-parallelStreamm循環用時:17 ms
    -----------------------------------------
    數據條數:1000
    普通for循環用時:1262 ms
    增強for循環用時:1263 ms
    forEach循環用時:1263 ms
    forEach-Stream循環用時:1264 ms
    forEach-parallelStreamm循環用時:159 ms
    -----------------------------------------
    數據條數:10000
    普通for循環用時:12640 ms
    增強for循環用時:12617 ms
    forEach循環用時:12653 ms
    forEach-Stream循環用時:12636 ms
    forEach-parallelStreamm循環用時:1581 ms
    -----------------------------------------
    數據條數:100000
    普通for循環用時:126229 ms
    增強for循環用時:127097 ms
    forEach循環用時:126671 ms
    forEach-Stream循環用時:126742 ms
    forEach-parallelStreamm循環用時:15791 ms

    Process finished with exit code 0

      

     

    posted on 2022-06-28 16:22  月染霜華  閱讀(1)  評論(0編輯  收藏  舉報
    国产美女a做受大片观看