perf.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  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. #pragma once
  15. #include "ultra_infer/utils/utils.h"
  16. #include <chrono> // NOLINT
  17. namespace ultra_infer {
  18. class ULTRAINFER_DECL TimeCounter {
  19. public:
  20. void Start() { begin_ = std::chrono::system_clock::now(); }
  21. void End() { end_ = std::chrono::system_clock::now(); }
  22. double Duration() {
  23. auto duration =
  24. std::chrono::duration_cast<std::chrono::microseconds>(end_ - begin_);
  25. return static_cast<double>(duration.count()) *
  26. std::chrono::microseconds::period::num /
  27. std::chrono::microseconds::period::den;
  28. }
  29. void PrintInfo(const std::string &prefix = "TimeCounter: ",
  30. bool print_out = true) {
  31. if (!print_out) {
  32. return;
  33. }
  34. FDLogger() << prefix << " duration = " << Duration() << "s." << std::endl;
  35. }
  36. private:
  37. std::chrono::time_point<std::chrono::system_clock> begin_;
  38. std::chrono::time_point<std::chrono::system_clock> end_;
  39. };
  40. } // namespace ultra_infer