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 namespace panda::ecmascript { 23 class ClockScope { 24 using Clock = std::chrono::high_resolution_clock; 25 using Duration = std::chrono::duration<uint64_t, std::nano>; 26 27 public: ClockScope()28 ClockScope() 29 { 30 start_ = Clock::now(); 31 } 32 GetPauseTime()33 Duration GetPauseTime() const 34 { 35 return Clock::now() - start_; 36 } 37 TotalSpentTime()38 float TotalSpentTime() const 39 { 40 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - start_); 41 return (float) duration.count() / MILLION_TIME; 42 } 43 GetCurTime()44 double GetCurTime() const 45 { 46 auto curTime_ = Duration(start_.time_since_epoch()); 47 return (double) curTime_.count() / MILLION_TIME; 48 } 49 50 private: 51 NO_COPY_SEMANTIC(ClockScope); 52 NO_MOVE_SEMANTIC(ClockScope); 53 54 Clock::time_point start_; 55 static constexpr uint32_t MILLION_TIME = 1000; 56 }; 57 } // namespace panda::ecmascript 58 #endif // ECMASCRIPT_MEM_CLOCK_SCOPE_H 59