StopWatch.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #define LOG_TAG "StopWatch"
  17. #include <utils/StopWatch.h>
  18. /* for PRId64 */
  19. #ifndef __STDC_FORMAT_MACROS
  20. #define __STDC_FORMAT_MACROS 1
  21. #endif
  22. #include <inttypes.h>
  23. #include <utils/Log.h>
  24. /*****************************************************************************/
  25. namespace android {
  26. StopWatch::StopWatch(const char* name, int clock) : mName(name), mClock(clock) {
  27. reset();
  28. }
  29. StopWatch::~StopWatch()
  30. {
  31. nsecs_t elapsed = elapsedTime();
  32. const int n = mNumLaps;
  33. ALOGD("StopWatch %s (us): %" PRId64 " ", mName, ns2us(elapsed));
  34. for (int i=0 ; i<n ; i++) {
  35. const nsecs_t soFar = mLaps[i].soFar;
  36. const nsecs_t thisLap = mLaps[i].thisLap;
  37. ALOGD(" [%d: %" PRId64 ", %" PRId64, i, ns2us(soFar), ns2us(thisLap));
  38. }
  39. }
  40. const char* StopWatch::name() const
  41. {
  42. return mName;
  43. }
  44. nsecs_t StopWatch::lap()
  45. {
  46. nsecs_t elapsed = elapsedTime();
  47. if (mNumLaps >= 8) {
  48. elapsed = 0;
  49. } else {
  50. const int n = mNumLaps;
  51. mLaps[n].soFar = elapsed;
  52. mLaps[n].thisLap = n ? (elapsed - mLaps[n-1].soFar) : elapsed;
  53. mNumLaps = n+1;
  54. }
  55. return elapsed;
  56. }
  57. nsecs_t StopWatch::elapsedTime() const
  58. {
  59. return systemTime(mClock) - mStartTime;
  60. }
  61. void StopWatch::reset()
  62. {
  63. mNumLaps = 0;
  64. mStartTime = systemTime(mClock);
  65. }
  66. /*****************************************************************************/
  67. }; // namespace android