zoukankan      html  css  js  c++  java
  • 计算程序运行时间

    package org.tinygroup.demo;
    
    public class Timing {
        public static double round(double value) {
            return Math.round(value * 10.0) / 10.0;// 利用Math类的round方法进行四舍五入计算
        }
        
        public static String getElapsedText(long elapsedMillis) {
            if (elapsedMillis < 60000) {
                double unit = round(elapsedMillis / 1000.0);// 将时间转换成秒
                return unit + "秒";// 在转换完的时间后增加单位
            } else if (elapsedMillis < 60000 * 60) {
                double unit = round(elapsedMillis / 60000.0);// 将时间转换成分
                return unit + "分";// 在转换完的时间后增加单位
            } else if (elapsedMillis < 60000 * 60 * 24) {
                double unit = round(elapsedMillis / (60000.0 * 60));// 将时间转换成时
                return unit + "时";// 在转换完的时间后增加单位
            } else {
                double unit = round(elapsedMillis / (60000.0 * 60 * 24));// 将时间转换成天
                return unit + "天";// 在转换完的时间后增加单位
            }
        }
        
        public static void main(String[] args) {
            long begin = System.currentTimeMillis();
            System.out.println("程序开始运行时间:" + begin);
            for (int i = 0; i < 1000000000; i++) {
                Math.random();
            }
            long end = System.currentTimeMillis();
            System.out.println("程序结束运行时间:" + end);
            System.out.println("程序运行时间:" + getElapsedText(end - begin));
        }
    }

    关键:

      通过System类获取系统的当前时间。

  • 相关阅读:
    Shell命令之文本操作
    乘法表
    万年历
    猜数游戏
    Linux下如何进行FTP安装与设置
    CentOS 安装nload(流量统计)
    linux下创建用户并且限定用户主目录
    ftp 解决不能上传问题
    【题解】[TJOI2018]数学计算
    【平衡树做题记录】
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/12259898.html
Copyright © 2011-2022 走看看