获得毫秒在android系统decive显示 [英] get millisecond to display in android decive

查看:537
本文介绍了获得毫秒在android系统decive显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code。

int hours = (int) (timeElapsed / 3600000);
int minutes = (int) (timeElapsed - hours * 3600000) / 60000;
int seconds = (int) (timeElapsed - hours * 3600000 - minutes * 60000) / 1000;

我现在需要计算毫秒运行的真快。

I need to now calculate MilliSeconds that runs really fast.

请帮助我如何?

推荐答案

一至加快事物的方法是通过重用一些中间计算的,以减少运行时间的数学运算。添加一些常量,其计算在编译时完成后,将增加可读性。假设timeElapsed是一个int,还可以去除铸件。

One way to speed up things is to minimize the run-time mathematical operations by reusing some of the intermediate calculations. Adding some constants, whose calculation is done at compile-time, will increase readability. Assuming timeElapsed is an int, you can also remove the casting.

    final int MILLISECONDS_PER_SECOND = 1000;
    final int MILLISECONDS_PER_MINUTE = 60 * MILLISECONDS_PER_SECOND;
    final int MILLISECONDS_PER_HOUR = 60 * MILLISECONDS_PER_MINUTE;

    int hours = timeElapsed / MILLISECONDS_PER_HOUR;
    timeElapsed -= hours * MILLISECONDS_PER_HOUR;

    int minutes = timeElapsed / MILLISECONDS_PER_MINUTE;
    timeElapsed -= minutes * MILLISECONDS_PER_MINUTE;

    int seconds = timeElapsed / MILLISECONDS_PER_SECOND;
    timeElapsed -= seconds * MILLISECONDS_PER_SECOND;

    int millis = timeElapsed;

最后两行可以合并为一个多一点速度,但对称看上去还不错。 : - )

The last two lines could be combined for a bit more speed, but the symmetry looked nice. :-)

这篇关于获得毫秒在android系统decive显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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