首页 > 技术文章 > 如何计算java程序运行花了多长时间。加时间戳。

magicya 2017-04-08 14:58 原文

 

1 long start = System.currentTimeMillis(); // 记录起始时间
2 try { 
3      Thread.sleep(5000);                     // 线程睡眠5秒,让运行时间不那么小
4    } catch (InterruptedException e) {
5             e.printStackTrace();
6        }
7 long end = System.currentTimeMillis();       // 记录结束时间
8 System.out.println(end-start);              // 相减得出运行时间

单位是毫秒。

long start = System.currentTimeMillis(); //获取当前时间

long end = System.currentTimeMillis();  

 

//System.nanoTime()   

推荐阅读