小编Joh*_*hnF的帖子

Java循环效率

我正在比较Java中嵌套for,while和do-while循环的效率,并且我遇到了一些我需要帮助理解的奇怪结果.

public class Loops {
    public static void main(String[] args) {
        int L = 100000;    // number of iterations per loop
        // for loop
        double start = System.currentTimeMillis();
        long s1 = 0;
        for (int i=0; i < L; i++) {
            for (int j = 0; j < L; j++) {
                s1 += 1;
            }
        }
        double end = System.currentTimeMillis();
        String result1 = String.format("for loop: %.5f", (end-start) / 1000);
        System.out.println(s1);
        System.out.println(result1);

        // do-while loop
        double start1 = System.currentTimeMillis();
        int i = …
Run Code Online (Sandbox Code Playgroud)

java performance for-loop while-loop do-while

11
推荐指数
1
解决办法
6601
查看次数

标签 统计

do-while ×1

for-loop ×1

java ×1

performance ×1

while-loop ×1