如何使用System.currentTimeMillis()以秒为单位测量时间跨度? [英] How to measure the a time-span in seconds using System.currentTimeMillis()?

查看:203
本文介绍了如何使用System.currentTimeMillis()以秒为单位测量时间跨度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 System.currentTimeMillis(); 转换为秒?

long start6=System.currentTimeMillis();
System.out.println(counter.countPrimes(100000000)+" for "+start6);

控制台显示 5761455,1307816001290
我无法读取多少秒。

The console shows me 5761455 for 1307816001290. I can't read how many seconds that is.

任何帮助?

推荐答案

long start = System.currentTimeMillis();
counter.countPrimes(1000000);
long end = System.currentTimeMillis();

System.out.println("Took : " + ((end - start) / 1000));

更新

更准确的解决方案是:

final long start = System.nanoTime();
counter.countPrimes(1000000);
final long end = System.nanoTime();

System.out.println("Took: " + ((end - start) / 1000000) + "ms);

这篇关于如何使用System.currentTimeMillis()以秒为单位测量时间跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆