YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
timing.hpp
浏览该文件的文档.
1 /*
2  Copyright (C) by Franksoft 2012.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
33 #ifndef YB_INC_YTEST_TIMING_HPP_
34 #define YB_INC_YTEST_TIMING_HPP_ 1
35 
36 #include "../ydef.h"
37 #include <ctime>
38 
43 namespace ytest
44 {
45 
47 using ystdex::size_t;
48 
49 
54 namespace timing
55 {
56 
62 template<typename _fNow, typename _fCallable, typename... _tParams>
63 inline auto
64 once(_fNow now, _fCallable&& f, _tParams&&... args) -> decltype(now() - now())
65 {
66  const auto cl(now());
67 
68  yforward(f)(yforward(args)...);
69  return now() - cl;
70 }
71 
77 template<typename _fCallable, typename... _tParams>
78 inline double
79 once_c(_fCallable&& f, _tParams&&... args)
80 {
81  const std::clock_t cl(std::clock());
82 
83  yforward(f)(yforward(args)...);
84  return double(clock() - cl) / CLOCKS_PER_SEC;
85 }
86 
87 
94 template<class _fNow, typename _fCallable, typename... _tParams>
95 inline auto
96 total(size_t n, _fNow now, _fCallable&& f, _tParams&&... args)
97  -> decltype(now() - now())
98 {
99  const auto cl(now());
100 
101  for(size_t i(0); i != n; ++i)
102  yforward(f)(yforward(args)...);
103  return now() - cl;
104 }
105 
111 template<typename _fCallable, typename... _tParams>
112 inline double
113 total_c(size_t n, _fCallable&& f, _tParams&&... args)
114 {
115  const std::clock_t cl(std::clock());
116 
117  for(size_t i(0); i != n; ++i)
118  yforward(f)(yforward(args)...);
119  return double(clock() - cl) / CLOCKS_PER_SEC;
120 }
121 
122 
127 template<typename... _tParams>
128 inline auto
129 average(size_t n, _tParams&&... args)
130  -> decltype(timing::total(n, yforward(args)...) / n)
131 {
132  return timing::total(n, yforward(args)...) / n;
133 }
134 
135 } // namespace timing;
136 
137 } // namespace ytest;
138 
139 #endif
140