使用Java实现三种精度的秒表(计时器)

1 引入

众所周知,链表和数组各有优缺点,其中增删改查操作速度就是显著差距之一,在学习ArrayListLinkedList时,我想用代码得到它们进行相同操作(如add(int index, E element)get(int index)等)时所耗时间的具体差距进一步证明它们的优缺点,所以此时就产生了一个秒表 的必要性。(此秒表存在≤20ms的误差)

我想实现的效果:

public static void main(String[] args){
   
	秒表 sw = new 秒表();
	sw.开始计时();
	n行代码执行......
	sw.结束计时并返回计时结果();
	System.out.println("程序执行耗时="+计时结果)
}

话不多说,开始一步一步制作这个Stopwatch类。

2 计时第一步——获取时间

在Java的System中提供了两种获取时间的方法——System.currentTimeMilis();System.nanoTime();

[关于这两种方法]1

public static long currentTimeMillis()

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
返回当前时间(以毫秒为单位)。 注意:虽然返回值的时间单位为毫秒,但该值的精度取决于底层操作系统,并且可能较大。 例如,许多操作系统以几十毫秒为单位测量时间。

返回值:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
返回由毫秒测量的从1970/1/1 UTC. 到 现在 UTC.的间隔(UTC.:协调世界时)2


public static long nanoTime()

Returns the current value of the running Java Virtual Machine’s high-resolution time source, in nanoseconds.
以纳秒为单位返回运行中的Java虚拟机高分辨率的当前时间

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.

此方法只能用于测量一段与任何系统时间或钟表时间无关的经过时间。返回一个用纳秒表示的从一个固定但不确定的起始时间(可能在将来,所以返回的值可能是负数)。 在Java虚拟机中,该方法的所有调用都使用相同的来源; 其他虚拟机实例可能会使用不同的源。

This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis().

该方法提供纳秒精度,但分辨率不一定是纳秒(即值的变化频率) - 除了分辨率至少与currentTimeMillis()的分辨率不同之外,不作任何其他保证 。

Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not correctly compute elapsed time due to numerical overflow.
跨越大约292年( 2 63 2^{63} 263纳秒)的差异将无法正确计算由于数字溢出所造成的时间。

The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine, is computed.
仅当在Java虚拟机的同一实例中获得的两个此类值之间的差异被计算时,此方法返回的值才会有意义。

返回值:
the current value of the running Java Virtual Machine’s high-resolution time source, in nanoseconds
运行中的Java虚拟机高分辨率的当前时间

3 计时第二步——得到结果

所以,我们至少可以实现两种不同精度的计时——纳秒级和毫秒级

计时思路:
记 录 开 始 时 间 → . . . → 记 录 结 束 时 间 记录开始时间 \to... \to 记录结束时间 ...
此时结果:
( 结 束 时 间 − 开 始 时 间 ) → 进 率 转 换 \lparen 结束时间 - 开始时间 \rparen \to 进率转换 ()

而此时就可以进行思路扩展——加入暂停计时功能
计时思路:
记 录 开 始 时 间 → . . . → 记 录 暂 停 开 始 时 间 → . . . → 记 录 暂 停 结 束 时 间 → . . . → 记 录 结 束 时 间 记录开始时间\to...\to记录暂停开始时间\to...\to记录暂停结束时间\to...\to记录结束时间 .........
此时结果:
{ 结 束 时 间 − [ 开 始 时 间 − ( 暂 停 开 始 时 间 − 暂 停 结 束 时 间 ) ] } → 进 率 转 换 \lbrace 结束时间 - [开始时间-(暂停开始时间-暂停结束时间)] \rbrace \to 进率转换 { [()]}

4. 具体实现

4.1 秒表的状态

用枚举定义秒表的暂停,运行,停止的三种状态。

代码实现

mystop

package my_stopwatch.enums;

public enum SWStatus {
   
    running,
    stopped,
    paused
}

4.2 秒表运行流程

运行流程如下图所示:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值