StopWatch.cpp revision 9d4536835248525f32f1504a3d28d5bbfa0a2910
1/*
2 * Copyright (C) 2005 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#define LOG_TAG "StopWatch"
18
19#include <string.h>
20#include <stdlib.h>
21#include <stdio.h>
22
23#include <utils/Log.h>
24#include <utils/Errors.h>
25#include <utils/StopWatch.h>
26
27/*****************************************************************************/
28
29namespace android {
30
31
32StopWatch::StopWatch(const char *name, int clock, uint32_t flags)
33    :   mName(name), mClock(clock), mFlags(flags)
34{
35    reset();
36}
37
38StopWatch::~StopWatch()
39{
40    nsecs_t elapsed = elapsedTime();
41    const int n = mNumLaps;
42    ALOGD("StopWatch %s (us): %lld ", mName, ns2us(elapsed));
43    for (int i=0 ; i<n ; i++) {
44        const nsecs_t soFar = mLaps[i].soFar;
45        const nsecs_t thisLap = mLaps[i].thisLap;
46        ALOGD(" [%d: %lld, %lld]", i, ns2us(soFar), ns2us(thisLap));
47    }
48}
49
50const char* StopWatch::name() const
51{
52    return mName;
53}
54
55nsecs_t StopWatch::lap()
56{
57    nsecs_t elapsed = elapsedTime();
58    if (mNumLaps >= 8) {
59        elapsed = 0;
60    } else {
61        const int n = mNumLaps;
62        mLaps[n].soFar   = elapsed;
63        mLaps[n].thisLap = n ? (elapsed - mLaps[n-1].soFar) : elapsed;
64        mNumLaps = n+1;
65    }
66    return elapsed;
67}
68
69nsecs_t StopWatch::elapsedTime() const
70{
71    return systemTime(mClock) - mStartTime;
72}
73
74void StopWatch::reset()
75{
76    mNumLaps = 0;
77    mStartTime = systemTime(mClock);
78}
79
80
81/*****************************************************************************/
82
83}; // namespace android
84
85