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