• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_MEM_CLOCK_SCOPE_H
17 #define ECMASCRIPT_MEM_CLOCK_SCOPE_H
18 
19 #include <ctime>
20 #include <chrono>
21 
22 #include "libpandabase/macros.h"
23 
24 namespace panda::ecmascript {
25 class ClockScope {
26 using Clock = std::chrono::high_resolution_clock;
27 using Duration = std::chrono::duration<uint64_t, std::nano>;
28 
29 public:
ClockScope()30     ClockScope()
31     {
32         start_ = Clock::now();
33     }
34 
Reset()35     void Reset()
36     {
37         start_ = Clock::now();
38     }
39 
GetPauseTime()40     Duration GetPauseTime() const
41     {
42         return Clock::now() - start_;
43     }
44 
TotalSpentTime()45     float TotalSpentTime() const
46     {
47         auto duration = std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - start_);
48         return (float) duration.count() / MILLION_TIME;
49     }
50 
TotalSpentTimeInMicroseconds()51     int TotalSpentTimeInMicroseconds() const
52     {
53         return std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - start_).count();
54     }
55 
GetCurTime()56     double GetCurTime() const
57     {
58         auto curTime_ = Duration(start_.time_since_epoch());
59         return (double) curTime_.count() / MILLION_TIME;
60     }
61 
62 private:
63     NO_COPY_SEMANTIC(ClockScope);
64     NO_MOVE_SEMANTIC(ClockScope);
65 
66     Clock::time_point start_;
67     static constexpr uint32_t MILLION_TIME = 1000;
68 };
69 }  // namespace panda::ecmascript
70 #endif  // ECMASCRIPT_MEM_CLOCK_SCOPE_H
71