StopWatch.java revision 7687f05f7bd02a4400e340eeb05cd296c34da42a
1/* 2 * Copyright (C) 2010 The Android Open Source Project 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 17package com.android.email; 18 19import android.os.SystemClock; 20import android.util.Log; 21 22/** 23 * A simple class to measure elapsed time. 24 * 25 * <code> 26 * StopWatch s = StopWatch.start(); 27 * // Do your stuff 28 * s.split(); 29 * // More stuff 30 * s.split(); 31 * // More stuff 32 * s.stop(); 33 * </code> 34 */ 35public class StopWatch { 36 private final String mName; 37 private final long mStart; 38 private long mLastSplit; 39 40 private StopWatch(String name) { 41 mName = name; 42 mStart = getCurrentTime(); 43 mLastSplit = mStart; 44 Log.w(Email.LOG_TAG, "StopWatch(" + mName + ") start"); 45 } 46 47 public static StopWatch start(String name) { 48 return new StopWatch(name); 49 } 50 51 public void split(String label) { 52 long now = getCurrentTime() ; 53 long elapse = now - mLastSplit; 54 Log.w(Email.LOG_TAG, "StopWatch(" + mName + ") split(" + label + ") " + elapse); 55 mLastSplit = now; 56 } 57 58 public void stop() { 59 long now = getCurrentTime(); 60 long elapse = now - mLastSplit; 61 Log.w(Email.LOG_TAG, "StopWatch(" + mName + ") stop: " 62 + (now - mLastSplit) 63 + " (total " + (now - mStart) + ")"); 64 } 65 66 private static long getCurrentTime() { 67 // We might want to use other counters, such as currentThreadTimeMillis(). 68 // TODO add option for that? 69 return SystemClock.elapsedRealtime(); 70 } 71} 72