gpt4 book ai didi

java - 如何计算程序运行时消耗了多少时间

转载 作者:行者123 更新时间:2023-12-01 06:11:17 25 4
gpt4 key购买 nike

我正在尝试计算程序消耗的时间。但是下面显示的两种方法有什么区别呢?

System.currentTimeMillis() % 1000
System.currentTimeMillis() / 1000

最佳答案

我假设您要除以 1000,您希望在几秒钟内得到结果?无论如何,模数运算符 % 并不是您想要的,它给出了除以第二个操作数的余数。

要获取某些代码的运行时间,请获取执行前和执行后的当前时间。运行时将是两者之间的差异。

System timeBefore = System.currentTimeMillis();
//PUT CODE HERE
System timeAfter = System.currentTimeMillis();

System timeDelta = timeAfter = timeBefore;
System.out.println("Runtime was " + timeDelta + " millis"); //display milliseconds
System.out.println("Runtime was " + (timeDelta / 1000) + " seconds"); //display seconds

关于java - 如何计算程序运行时消耗了多少时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com