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