util_time.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __UTIL_TIME_H__
  17. #define __UTIL_TIME_H__
  18. #include "util/util_string.h"
  19. CCL_NAMESPACE_BEGIN
  20. /* Give current time in seconds in double precision, with good accuracy. */
  21. double time_dt();
  22. /* Sleep for the specified number of seconds. */
  23. void time_sleep(double t);
  24. /* Scoped timer. */
  25. class scoped_timer {
  26. public:
  27. explicit scoped_timer(double *value = NULL) : value_(value)
  28. {
  29. time_start_ = time_dt();
  30. }
  31. ~scoped_timer()
  32. {
  33. if (value_ != NULL) {
  34. *value_ = get_time();
  35. }
  36. }
  37. double get_start() const
  38. {
  39. return time_start_;
  40. }
  41. double get_time() const
  42. {
  43. return time_dt() - time_start_;
  44. }
  45. protected:
  46. double *value_;
  47. double time_start_;
  48. };
  49. /* Make human readable string from time, compatible with Blender metadata. */
  50. string time_human_readable_from_seconds(const double seconds);
  51. double time_human_readable_to_seconds(const string &str);
  52. CCL_NAMESPACE_END
  53. #endif