日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java stopwatch 功能

發布時間:2025/4/9 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java stopwatch 功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C#中有一個stopwatch的功能,主要是用來監測程序執行時間的。java之前一直都在用如下方式完成:

1 public static void main(String[] args) { 2 long startTime=System.currentTimeMillis(); //獲取開始時間 3 4 //函數主體代碼 5 //... 6 7 long endTime=System.currentTimeMillis(); //獲取結束時間 8 System.out.println("程序運行時間: "+(endTime-startTime)+"ms"); 9 10 } 原生監測方式

今天上網搜索了一下,找到了一個比較類似的:

1 import org.apache.commons.lang3.time; 2 3 StopWatch watch=new StopWatch(); 4 watch.start(); 5 watch.stop(); 6 watch.getSplitTime(); apache.commons.lang3

但是上面的時間處理只支持ms,有些時間比較長還得自己處理,就又找了一個:

1 import java.util.concurrent.TimeUnit; 2 import com.google.common.base.Stopwatch; 3 4 Stopwatch watch = new Stopwatch(); 5 watch.start(); 6 watch.stop(); 7 watch.elapsed(TimeUnit.MINUTES); google stopwatch

時間可以支持多種格式,可以自由選擇,但是看了下源碼,感覺里面好多方法都是? @Deprecated,估計是比較老的api了:

1 /* 2 * Copyright (C) 2008 The Guava Authors 3 * 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 at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * 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 and 14 * limitations under the License. 15 */ 16 17 package com.google.common.base; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 import static com.google.common.base.Preconditions.checkState; 21 import static java.util.concurrent.TimeUnit.MICROSECONDS; 22 import static java.util.concurrent.TimeUnit.MILLISECONDS; 23 import static java.util.concurrent.TimeUnit.NANOSECONDS; 24 import static java.util.concurrent.TimeUnit.SECONDS; 25 26 import com.google.common.annotations.Beta; 27 import com.google.common.annotations.GwtCompatible; 28 import com.google.common.annotations.GwtIncompatible; 29 30 import java.util.concurrent.TimeUnit; 31 32 /** 33 * An object that measures elapsed time in nanoseconds. It is useful to measure 34 * elapsed time using this class instead of direct calls to {@link 35 * System#nanoTime} for a few reasons: 36 * 37 * <ul> 38 * <li>An alternate time source can be substituted, for testing or performance 39 * reasons. 40 * <li>As documented by {@code nanoTime}, the value returned has no absolute 41 * meaning, and can only be interpreted as relative to another timestamp 42 * returned by {@code nanoTime} at a different time. {@code Stopwatch} is a 43 * more effective abstraction because it exposes only these relative values, 44 * not the absolute ones. 45 * </ul> 46 * 47 * <p>Basic usage: 48 * <pre> 49 * Stopwatch stopwatch = new Stopwatch().{@link #start start}(); 50 * doSomething(); 51 * stopwatch.{@link #stop stop}(); // optional 52 * 53 * long millis = stopwatch.elapsed(MILLISECONDS); 54 * 55 * log.info("that took: " + stopwatch); // formatted string like "12.3 ms" 56 * </pre> 57 * 58 * <p>Stopwatch methods are not idempotent; it is an error to start or stop a 59 * stopwatch that is already in the desired state. 60 * 61 * <p>When testing code that uses this class, use the {@linkplain 62 * #Stopwatch(Ticker) alternate constructor} to supply a fake or mock ticker. 63 * <!-- TODO(kevinb): restore the "such as" --> This allows you to 64 * simulate any valid behavior of the stopwatch. 65 * 66 * <p><b>Note:</b> This class is not thread-safe. 67 * 68 * @author Kevin Bourrillion 69 * @since 10.0 70 */ 71 @Beta 72 @GwtCompatible(emulated = true) 73 public final class Stopwatch { 74 private final Ticker ticker; 75 private boolean isRunning; 76 private long elapsedNanos; 77 private long startTick; 78 79 /** 80 * Creates (but does not start) a new stopwatch using {@link System#nanoTime} 81 * as its time source. 82 */ 83 public Stopwatch() { 84 this(Ticker.systemTicker()); 85 } 86 87 /** 88 * Creates (but does not start) a new stopwatch, using the specified time 89 * source. 90 */ 91 public Stopwatch(Ticker ticker) { 92 this.ticker = checkNotNull(ticker, "ticker"); 93 } 94 95 /** 96 * Returns {@code true} 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 boolean isRunning() { 101 return isRunning; 102 } 103 104 /** 105 * Starts the stopwatch. 106 * 107 * @return this {@code Stopwatch} instance 108 * @throws IllegalStateException if the stopwatch is already running. 109 */ 110 public Stopwatch 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 had 120 * elapsed up to this point. 121 * 122 * @return this {@code Stopwatch} instance 123 * @throws IllegalStateException if the stopwatch is already stopped. 124 */ 125 public Stopwatch 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 * @return this {@code Stopwatch} instance 139 */ 140 public Stopwatch reset() { 141 elapsedNanos = 0; 142 isRunning = false; 143 return this; 144 } 145 146 private long elapsedNanos() { 147 return isRunning ? ticker.read() - startTick + elapsedNanos : elapsedNanos; 148 } 149 150 /** 151 * Returns the current elapsed time shown on this stopwatch, expressed 152 * in the desired time unit, with any fraction rounded down. 153 * 154 * <p>Note that the overhead of measurement can be more than a microsecond, so 155 * it is generally not useful to specify {@link TimeUnit#NANOSECONDS} 156 * precision here. 157 * 158 * @since 14.0 (since 10.0 as {@code elapsedTime()}) 159 */ 160 public long elapsed(TimeUnit desiredUnit) { 161 return desiredUnit.convert(elapsedNanos(), NANOSECONDS); 162 } 163 164 /** 165 * Returns the current elapsed time shown on this stopwatch, expressed 166 * in the desired time unit, with any fraction rounded down. 167 * 168 * <p>Note that the overhead of measurement can be more than a microsecond, so 169 * it is generally not useful to specify {@link TimeUnit#NANOSECONDS} 170 * precision here. 171 * 172 * @deprecated Use {@link Stopwatch#elapsed(TimeUnit)} instead. This method is 173 * scheduled to be removed in Guava release 16.0. 174 */ 175 @Deprecated 176 public long elapsedTime(TimeUnit desiredUnit) { 177 return elapsed(desiredUnit); 178 } 179 180 /** 181 * Returns the current elapsed time shown on this stopwatch, expressed 182 * in milliseconds, with any fraction rounded down. This is identical to 183 * {@code elapsed(TimeUnit.MILLISECONDS)}. 184 * 185 * @deprecated Use {@code stopwatch.elapsed(MILLISECONDS)} instead. This 186 * method is scheduled to be removed in Guava release 16.0. 187 */ 188 @Deprecated 189 public long elapsedMillis() { 190 return elapsed(MILLISECONDS); 191 } 192 193 /** 194 * Returns a string representation of the current elapsed time. 195 */ 196 @GwtIncompatible("String.format()") 197 @Override public String toString() { 198 return toString(4); 199 } 200 201 /** 202 * Returns a string representation of the current elapsed time, choosing an 203 * appropriate unit and using the specified number of significant figures. 204 * For example, at the instant when {@code elapsed(NANOSECONDS)} would 205 * return {1234567}, {@code toString(4)} returns {@code "1.235 ms"}. 206 * 207 * @deprecated Use {@link #toString()} instead. This method is scheduled 208 * to be removed in Guava release 15.0. 209 */ 210 @Deprecated 211 @GwtIncompatible("String.format()") 212 public String toString(int significantDigits) { 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(long nanos) { 224 if (SECONDS.convert(nanos, NANOSECONDS) > 0) { 225 return SECONDS; 226 } 227 if (MILLISECONDS.convert(nanos, NANOSECONDS) > 0) { 228 return MILLISECONDS; 229 } 230 if (MICROSECONDS.convert(nanos, NANOSECONDS) > 0) { 231 return MICROSECONDS; 232 } 233 return NANOSECONDS; 234 } 235 236 private static String abbreviate(TimeUnit unit) { 237 switch (unit) { 238 case NANOSECONDS: 239 return "ns"; 240 case MICROSECONDS: 241 return "\u03bcs"; // 渭s 242 case MILLISECONDS: 243 return "ms"; 244 case SECONDS: 245 return "s"; 246 default: 247 throw new AssertionError(); 248 } 249 } 250 } google stopwatch source code

?

  

轉載于:https://www.cnblogs.com/lzxianren/p/4761537.html

總結

以上是生活随笔為你收集整理的java stopwatch 功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 香蕉视频污视频 | 日韩欧美字幕 | 色哟哟黄色 | 麻豆tv在线观看 | 欧洲成人av | 人妻少妇精品无码专区久久 | 绿帽h啪肉np辣文 | 老女人综合网 | 在线精品亚洲欧美日韩国产 | 国产精品美女主播 | 色综合激情网 | 91天天爽| 波多野吉衣中文字幕 | 国产盗摄一区二区三区在线 | 四色最新网址 | 欧美日韩二三区 | 九九国产 | 2017狠狠干 | 免费爱爱网站 | 不卡一区在线 | 先锋成人| 激情综合影院 | 国产精品久久久久久久专区 | 成人a级网站 | 日韩av在线直播 | 欧美做受 | 臭脚猛1s民工调教奴粗口视频 | 亲子乱对白乱都乱了 | 免费播放毛片精品视频 | 91麻豆视频 | 鲁一鲁一鲁一鲁一av | 青娱乐国产在线视频 | 亚洲欧美日韩电影 | 天天操天天插 | 国产精品99久久久 | 中文字幕电影一区 | 老头老太吃奶xb视频 | 最新福利视频 | 岛国av免费在线 | 免费看黄色的网站 | 黄色一级在线观看 | 天天色亚洲| 日本色片网站 | 中文在线一区二区 | 国产精品久久久久久久久绿色 | 日韩av在线网站 | 三浦惠理子aⅴ一二三区 | 欧美成人精品欧美一级私黄 | 少妇人妻真实偷人精品视频 | 91九色蝌蚪| 人人插人人澡 | 手机在线播放av | 欧美一区视频在线 | 欧美一区二区三区在线看 | 色优久久| 91成人在线视频 | 九色porny自拍视频在线播放 | 最新av免费在线观看 | 五月色婷 | 少妇精品久久久久久久久久 | 麻豆一区二区三区精品视频 | 九九热精品免费视频 | 一级做a爰片 | caoporn免费在线 | 午夜av毛片 | 国产又黄视频 | 亚洲美女久久久 | 尤物视频最新网址 | 日韩激情成人 | 日韩在线观看免费高清 | 日美韩一区二区三区 | 婷婷五月花| 波多野结衣免费视频观看 | 日日色综合| 自拍偷拍第 | 老熟妇仑乱视频一区二区 | 成人午夜视频在线观看 | 亚洲免费中文字幕 | 欧美高清hd19 | 亚洲精品无码成人 | 深夜av| 久久久久久久久久久91 | 国产无套视频 | 久久亚洲伊人 | 琪琪色综合 | 色悠悠国产精品 | 国产又大又粗又长 | 欧美一区二区三区久久成人精品 | 极品白嫩丰满美女无套 | 91天天操| 日韩免费一级 | 小优视频污 | 一级黄色在线 | www.69av.com| 特级淫片aaaaaaa级 | 黄色国产在线 | 国产精品电影院 | 黑巨茎大战欧美白妞 | xxxx 国产 |