Java中while和for循环性能比较

Java中while和for循环性能比较

public static void main(String[] args) {
   

```java
 long startTime = System.currentTimeMillis();
    Map<String, Map<String, String>> map = new HashMap<>();
    int i = 0;
    int j = 0;
    while (i < 1000) {
        while (j < 1000) {
            Map<String, String> mapVal = new HashMap<>();
            mapVal.put("addressTypeID", Math.random() * 5 + "");
            mapVal.put("dataTypeNum", Math.random() * 5 + "");
            mapVal.put("variableAddress", Math.random() * 5 + "");
            mapVal.put("memoryType", Math.random() * 5 + "");
            mapVal.put("deviation", Math.random() * 5 + "");
            mapVal.put("variableID", Math.random() * 5 + "");
            mapVal.put("val", Math.random() * 5 + "");
            map.put(j + "", mapVal);
            j++;
        }
        i++;
    }
    long endTime = System.currentTimeMillis();
    System.out.println(endTime - startTime + "mm:while");
    long startTime1 = System.currentTimeMillis();
    Map<String, Map<String, String>> map1 = new HashMap<>();
    for (int k = 0; k < 1000; k++) {
        for (int l = 0; l < 1000; l++) {
            Map<String, String> mapVal = new HashMap<>();
            mapVal.put("addressTypeID", Math.random() * 5 + "");
            mapVal.put("dataTypeNum", Math.random() * 5 + "");
            mapVal.put("variableAddress", Math.random() * 5 + "");
            mapVal.put("memoryType", Math.random() * 5 + "");
            mapVal.put("deviation", Math.random() * 5 + "");
            mapVal.put("variableID", Math.random() * 5 + "");
            mapVal.put("val", Math.random() * 5 + "");
            map1.put(j + "", mapVal);
        }
    }
    long endTime1 = System.currentTimeMillis();
    System.out.println(endTime1 - startTime1 + "mm:for");

}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210412142422302.png#pic_center)


版权声明:本文为lanerwang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。