| 1 | // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
|---|---|
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #ifndef RUNTIME_VM_SCOPE_TIMER_H_ |
| 6 | #define RUNTIME_VM_SCOPE_TIMER_H_ |
| 7 | |
| 8 | #include "platform/allocation.h" |
| 9 | #include "platform/globals.h" |
| 10 | |
| 11 | #include "vm/os.h" |
| 12 | |
| 13 | namespace dart { |
| 14 | |
| 15 | // Simple utility class for timing a block of code. |
| 16 | class ScopeTimer : public ValueObject { |
| 17 | public: |
| 18 | explicit ScopeTimer(const char* name, bool enabled = true) |
| 19 | : enabled_(enabled), name_(name), start_(0) { |
| 20 | if (!enabled_) { |
| 21 | return; |
| 22 | } |
| 23 | start_ = OS::GetCurrentMonotonicMicros(); |
| 24 | } |
| 25 | |
| 26 | int64_t GetElapsed() const { |
| 27 | int64_t end = OS::GetCurrentMonotonicMicros(); |
| 28 | ASSERT(end >= start_); |
| 29 | return end - start_; |
| 30 | } |
| 31 | |
| 32 | ~ScopeTimer() { |
| 33 | if (!enabled_) { |
| 34 | return; |
| 35 | } |
| 36 | int64_t elapsed = GetElapsed(); |
| 37 | double seconds = MicrosecondsToSeconds(elapsed); |
| 38 | OS::PrintErr("%s: %f seconds (%"Pd64 " \u00B5s)\n", name_, seconds, |
| 39 | elapsed); |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | const bool enabled_; |
| 44 | const char* name_; |
| 45 | int64_t start_; |
| 46 | }; |
| 47 | |
| 48 | } // namespace dart |
| 49 | |
| 50 | #endif // RUNTIME_VM_SCOPE_TIMER_H_ |
| 51 |