执行循环需要多长时间? [英] How much time does it take to execute a loop?
本文介绍了执行循环需要多长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法知道在java中执行循环需要多少秒?
Is there any way to know how many seconds does it take a loop to execute in java?
例如:
for(int i=0; i < 1000000; i++) {
//Do some difficult task goes in here
}
它不必是100%准确,但它只是想知道可能需要多长时间。里面的算法是某种写入.txt文件的密钥生成器。我希望它甚至需要几分钟,所以对于我的第一次测试我想计算秒数。
It does not have to be accurate 100%, but its just to have an idea of how long it could take. The algorithm inside is some kind of key generator that writes to a .txt file. I expect it to take even a few mins, so for my first test i want to count the seconds.
推荐答案
在这里你可以试试这个:
Here you can try this:
long startTime = System.currentTimeMillis();
long endTime = 0;
for(int i=0; i < 1000000; i++) {
//Something
}
endTime = System.currentTimeMillis();
long timeneeded = ((startTime - endTime) /1000);
这篇关于执行循环需要多长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文