小编sam*_*can的帖子

每百万次迭代打印一次消息

我需要编写一个循环,每百万次迭代打印一次消息.我想让它运行10秒钟(时钟时间)来查看打印的语句数量.

我想我现在只是把自己绑在了结...

public class OneInAMillion{
  public static void main(String []args){
    long startTime = System.currentTimeMillis();  //time at start of execution
    long endTime = startTime + 10000;  //time at end of execution (10000 = 10 seconds)
    int count = 1;

    while(System.currentTimeMillis() < endTime) { //run this before 10 seconds is up
      for (int i = 0; i < count; i++) {
        if(i % 1000000 == 0) {
            System.out.println("Iteration: " + count++);  //print every millionth iteration
        }
      }
    }

    System.out.println("Time up!"); //print once …
Run Code Online (Sandbox Code Playgroud)

java iteration loops for-loop while-loop

0
推荐指数
1
解决办法
3147
查看次数

标签 统计

for-loop ×1

iteration ×1

java ×1

loops ×1

while-loop ×1