java stopwatch_java stopwatch 功能

java stopwatch_java stopwatch 功能1/*2*Copyright(C)2008TheGuavaAuthors3*4*LicensedundertheApacheLicense,Version2.0(the”License”);5*youmaynotusethisfileexceptincompliancewiththeLicense.6*Youmayobtain…

大家好,又见面了,我是你们的朋友全栈君。

1 /*

2 * Copyright (C) 2008 The Guava Authors3 *4 * Licensed under the Apache License, Version 2.0 (the “License”);5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 *http://www.apache.org/licenses/LICENSE-2.0

9 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an “AS IS” BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */

16

17 packagecom.google.common.base;18

19 import staticcom.google.common.base.Preconditions.checkNotNull;20 import staticcom.google.common.base.Preconditions.checkState;21 import staticjava.util.concurrent.TimeUnit.MICROSECONDS;22 import staticjava.util.concurrent.TimeUnit.MILLISECONDS;23 import staticjava.util.concurrent.TimeUnit.NANOSECONDS;24 import staticjava.util.concurrent.TimeUnit.SECONDS;25

26 importcom.google.common.annotations.Beta;27 importcom.google.common.annotations.GwtCompatible;28 importcom.google.common.annotations.GwtIncompatible;29

30 importjava.util.concurrent.TimeUnit;31

32 /**

33 * An object that measures elapsed time in nanoseconds. It is useful to measure34 * elapsed time using this class instead of direct calls to {@link

35 * System#nanoTime} for a few reasons:36 *37 *

  • 38 *
  • An alternate time source can be substituted, for testing or performance39 * reasons.40 *
  • As documented by {@codenanoTime}, the value returned has no absolute41 * meaning, and can only be interpreted as relative to another timestamp42 * returned by {@codenanoTime} at a different time. {@codeStopwatch} is a43 * more effective abstraction because it exposes only these relative values,44 * not the absolute ones.45 *

46 *47 *

Basic usage:48 *

49 *   Stopwatch stopwatch = new Stopwatch().{@link#start start}();50 *   doSomething();51 *   stopwatch.{@link#stop stop}(); // optional52 *53 *   long millis = stopwatch.elapsed(MILLISECONDS);54 *55 *   log.info("that took: " + stopwatch); // formatted string like "12.3 ms"56 * 

57 *58 *

Stopwatch methods are not idempotent; it is an error to start or stop a59 * stopwatch that is already in the desired state.60 *61 *

When testing code that uses this class, use the {@linkplain

62 * #Stopwatch(Ticker) alternate constructor} to supply a fake or mock ticker.63 * This allows you to64 * simulate any valid behavior of the stopwatch.65 *66 *

Note: This class is not thread-safe.67 *68 *@authorKevin Bourrillion69 *@since10.070 */

71 @Beta72 @GwtCompatible(emulated = true)73 public final classStopwatch {74 private finalTicker ticker;75 private booleanisRunning;76 private longelapsedNanos;77 private longstartTick;78

79 /**

80 * Creates (but does not start) a new stopwatch using {@linkSystem#nanoTime}81 * as its time source.82 */

83 publicStopwatch() {84 this(Ticker.systemTicker());85 }86

87 /**

88 * Creates (but does not start) a new stopwatch, using the specified time89 * source.90 */

91 publicStopwatch(Ticker ticker) {92 this.ticker = checkNotNull(ticker, “ticker”);93 }94

95 /**

96 * Returns {@codetrue} if {@link#start()} has been called on this stopwatch,97 * and {@link#stop()} has not been called since the last call to {@code

98 * start()}.99 */

100 public booleanisRunning() {101 returnisRunning;102 }103

104 /**

105 * Starts the stopwatch.106 *107 *@returnthis {@codeStopwatch} instance108 *@throwsIllegalStateException if the stopwatch is already running.109 */

110 publicStopwatch start() {111 checkState(!isRunning,112 “This stopwatch is already running; it cannot be started more than once.”);113 isRunning = true;114 startTick =ticker.read();115 return this;116 }117

118 /**

119 * Stops the stopwatch. Future reads will return the fixed duration that had120 * elapsed up to this point.121 *122 *@returnthis {@codeStopwatch} instance123 *@throwsIllegalStateException if the stopwatch is already stopped.124 */

125 publicStopwatch stop() {126 long tick =ticker.read();127 checkState(isRunning,128 “This stopwatch is already stopped; it cannot be stopped more than once.”);129 isRunning = false;130 elapsedNanos += tick -startTick;131 return this;132 }133

134 /**

135 * Sets the elapsed time for this stopwatch to zero,136 * and places it in a stopped state.137 *138 *@returnthis {@codeStopwatch} instance139 */

140 publicStopwatch reset() {141 elapsedNanos = 0;142 isRunning = false;143 return this;144 }145

146 private longelapsedNanos() {147 return isRunning ? ticker.read() – startTick +elapsedNanos : elapsedNanos;148 }149

150 /**

151 * Returns the current elapsed time shown on this stopwatch, expressed152 * in the desired time unit, with any fraction rounded down.153 *154 *

Note that the overhead of measurement can be more than a microsecond, so155 * it is generally not useful to specify {@linkTimeUnit#NANOSECONDS}156 * precision here.157 *158 *@since14.0 (since 10.0 as {@codeelapsedTime()})159 */

160 public longelapsed(TimeUnit desiredUnit) {161 returndesiredUnit.convert(elapsedNanos(), NANOSECONDS);162 }163

164 /**

165 * Returns the current elapsed time shown on this stopwatch, expressed166 * in the desired time unit, with any fraction rounded down.167 *168 *

Note that the overhead of measurement can be more than a microsecond, so169 * it is generally not useful to specify {@linkTimeUnit#NANOSECONDS}170 * precision here.171 *172 *@deprecatedUse {@linkStopwatch#elapsed(TimeUnit)} instead. This method is173 * scheduled to be removed in Guava release 16.0.174 */

175 @Deprecated176 public longelapsedTime(TimeUnit desiredUnit) {177 returnelapsed(desiredUnit);178 }179

180 /**

181 * Returns the current elapsed time shown on this stopwatch, expressed182 * in milliseconds, with any fraction rounded down. This is identical to183 * {@codeelapsed(TimeUnit.MILLISECONDS)}.184 *185 *@deprecatedUse {@codestopwatch.elapsed(MILLISECONDS)} instead. This186 * method is scheduled to be removed in Guava release 16.0.187 */

188 @Deprecated189 public longelapsedMillis() {190 returnelapsed(MILLISECONDS);191 }192

193 /**

194 * Returns a string representation of the current elapsed time.195 */

196 @GwtIncompatible(“String.format()”)197 @Override publicString toString() {198 return toString(4);199 }200

201 /**

202 * Returns a string representation of the current elapsed time, choosing an203 * appropriate unit and using the specified number of significant figures.204 * For example, at the instant when {@codeelapsed(NANOSECONDS)} would205 * return {1234567}, {@codetoString(4)} returns {@code”1.235 ms”}.206 *207 *@deprecatedUse {@link#toString()} instead. This method is scheduled208 * to be removed in Guava release 15.0.209 */

210 @Deprecated211 @GwtIncompatible(“String.format()”)212 public String toString(intsignificantDigits) {213 long nanos =elapsedNanos();214

215 TimeUnit unit =chooseUnit(nanos);216 double value = (double) nanos / NANOSECONDS.convert(1, unit);217

218 //Too bad this functionality is not exposed as a regular method call

219 return String.format(“%.” + significantDigits + “g %s”,220 value, abbreviate(unit));221 }222

223 private static TimeUnit chooseUnit(longnanos) {224 if (SECONDS.convert(nanos, NANOSECONDS) > 0) {225 returnSECONDS;226 }227 if (MILLISECONDS.convert(nanos, NANOSECONDS) > 0) {228 returnMILLISECONDS;229 }230 if (MICROSECONDS.convert(nanos, NANOSECONDS) > 0) {231 returnMICROSECONDS;232 }233 returnNANOSECONDS;234 }235

236 private staticString abbreviate(TimeUnit unit) {237 switch(unit) {238 caseNANOSECONDS:239 return “ns”;240 caseMICROSECONDS:241 return “\u03bcs”; //渭s

242 caseMILLISECONDS:243 return “ms”;244 caseSECONDS:245 return “s”;246 default:247 throw newAssertionError();248 }249 }250 }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152456.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js的数据类型有哪些?[通俗易懂]

    js的数据类型有哪些?[通俗易懂]数据类型一、数据类型:基本数据类型(值类型):字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)。引用数据类型(对象类型):对象(Object)、数组(Array)、函数(Function)。特殊的对象:正则(RegExp)和日期(Date)。特殊类型:underfined未定义、Null空对象、Infinate无穷、NAN非数字基本数据类型的值直接在栈内存中存储,值与值之间独立存在,修改一个变量不会影响.

    2022年9月5日
    3
  • 深入理解MySQL索引原理和实现——为什么索引可以加速查询?

    深入理解MySQL索引原理和实现——为什么索引可以加速查询?说到索引,很多人都知道“索引是一个排序的列表,在这个列表中存储着索引的值和包含这个值的数据所在行的物理地址,在数据十分庞大的时候,索引可以大大加快查询的速度,这是因为使用索引后可以不用扫描全表来定位某行的数据,而是先通过索引表找到该行数据对应的物理地址然后访问相应的数据。”但是索引是怎么实现的呢?因为索引并不是关系模型的组成部分,因此不同的DBMS有不同的实现,我们针对MySQL数据库的实现进…

    2022年6月24日
    39
  • 镁光闪存颗粒对照表_内存颗粒版本判断方法和编号解析(三星、美光、海力士)…

    镁光闪存颗粒对照表_内存颗粒版本判断方法和编号解析(三星、美光、海力士)…因为昨天chh看到一个人还在用土办法去判断美光颗粒版本加上前面B站也有人问我怎么去判断内存颗粒版本使用今天就抽空写一下。1.三星三星官方命名文件如下:实例:第一行:“SEC843”重要信息为843代表内存颗粒生产日期第二行:“K4A4G08”重要信息为4G08代表内存颗粒容量和位宽(AG代表容量为16Gb)第三行:“5WTBCTD”重要信息为T、TD,T代表颗粒版本我这个就是T-DI…

    2022年6月22日
    718
  • 关于kindeditor中如何设置富文本编辑器中的内容

    关于kindeditor中如何设置富文本编辑器中的内容关于kindeditor中如何设置富文本编辑器中的内容KindEditor.html(id,value);我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:全新的界面设计,将会带来全新的写作体验;在创作中心设置你喜爱的代码高亮样式,Markdown将代码片显示选择的高亮样式进行展示;增加了…

    2022年10月9日
    0
  • eXtremeDB_5noobs

    eXtremeDB_5noobseXtremeDB™ 3.1fromMcObject®ReleaseNotesTargetOS:HP-UX11forPA-RISC,ItaniumHostOS:HP-UX11.xBYUSINGTHISSOFTWAREYOUAGREETOMcObject’s LICENSEAGREEMENT

    2022年10月14日
    0
  • C++ | 矩阵类模板(类模板)

    C++ | 矩阵类模板(类模板)问题B:矩阵类模板(类模板)时间限制: 1Sec  内存限制: 128MB提交: 559  解决: 314[提交][状态][讨论版]题目描述设计一个矩阵类模板Matrix,支持任意数据类型的数据。要求至少包含2个成员函数:矩阵转置函数transport、以及打印输出函数print编写main函数进行测试,调用类的成员函数完成转置和输出。输入第一行先输入t,表示有t个测试用例从第二行开始输入…

    2022年5月14日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号