Created
April 24, 2013 06:06
-
-
Save YukiYoshikawa/5449976 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package trial.yy.guava.client.base; | |
| import com.google.common.base.Stopwatch; | |
| import java.util.Random; | |
| /** | |
| * com.google.common.base.Stopwatchを試すためのサンプル | |
| * User: yy | |
| * Date: 13/04/23 | |
| * Time: 23:01 | |
| */ | |
| public class StopWatchClient { | |
| public static void main(String[] args) { | |
| // Stopwatchを使わない場合の計測 | |
| long start = System.currentTimeMillis(); | |
| executeBusinessLogic(); | |
| long end = System.currentTimeMillis(); | |
| System.out.println("Execution time1: " + (end - start) + "ms"); | |
| // Stopwatchを使うとこんな感じ | |
| Stopwatch stopwatch1 = new Stopwatch().start(); | |
| executeBusinessLogic(); | |
| stopwatch1.stop(); | |
| System.out.println("Execution time2: " + stopwatch1); | |
| // 15.0からは コンストラクタの使用は非推奨になるかも | |
| // Stopwatch#createStarted or Stopwatch#createUnstartedを使うことになりそうです。 | |
| // リセットすると0に戻る | |
| stopwatch1.reset(); | |
| System.out.println("Execution time3: " + stopwatch1); | |
| // リセット後に再測定 | |
| stopwatch1.start(); | |
| executeBusinessLogic(); | |
| stopwatch1.stop(); | |
| System.out.println("Execution time4: " + stopwatch1); | |
| } | |
| /** | |
| * 何か業務処理の代わり | |
| */ | |
| private static void executeBusinessLogic() { | |
| Random r = new Random(); | |
| try { | |
| Thread.sleep(1000 * r.nextInt(4)); | |
| } catch (InterruptedException e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment